Skip to content

Commit

Permalink
introduce a native 'cache', eventually we will replace diskcache
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Dec 2, 2024
1 parent 672f91f commit 6c2e7f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion pioreactor/actions/led_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ def led_intensity(
logger.info(
f"Updated LED {channel} from {old_state[channel]:0.3g}% to {new_state[channel]:0.3g}%."
)

return updated_successfully


Expand Down
14 changes: 14 additions & 0 deletions pioreactor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def _initialize_table(self):
)
"""
)
self.conn.commit()

def __setitem__(self, key, value):
self.cursor.execute(
Expand All @@ -296,6 +297,7 @@ def __setitem__(self, key, value):
""",
(key, value),
)
self.conn.commit()

def get(self, key, default=None):
self.cursor.execute(f"SELECT value FROM {self.table_name} WHERE key = ?", (key,))
Expand All @@ -306,6 +308,17 @@ def iterkeys(self):
self.cursor.execute(f"SELECT key FROM {self.table_name}")
return (row[0] for row in self.cursor.fetchall())

def pop(self, key, default=None):
self.cursor.execute(f"SELECT value FROM {self.table_name} WHERE key = ?", (key,))
result = self.cursor.fetchone()
if result is None:
if default is None:
raise KeyError(f"Key '{key}' not found in cache.")
return default
self.cursor.execute(f"DELETE FROM {self.table_name} WHERE key = ?", (key,))
self.conn.commit()
return result[0]

def __contains__(self, key):
self.cursor.execute(f"SELECT 1 FROM {self.table_name} WHERE key = ?", (key,))
return self.cursor.fetchone() is not None
Expand All @@ -315,6 +328,7 @@ def __iter__(self):

def __delitem__(self, key):
self.cursor.execute(f"DELETE FROM {self.table_name} WHERE key = ?", (key,))
self.conn.commit()

def __getitem__(self, key):
self.cursor.execute(f"SELECT value FROM {self.table_name} WHERE key = ?", (key,))
Expand Down

0 comments on commit 6c2e7f7

Please sign in to comment.