Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved storage capabilities #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions signalbot/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@


class Storage:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making this class abstract to ensure the methods are implemented?

Suggested change
class Storage:
class Storage(ABC):

def __init__(self):
self.redis = None

Comment on lines +7 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the base Storage class needs to have the redis attribute defined. If anything, for consistency with the InMemoryStorage, it should be renamed to _storage

def exists(self, key: str) -> bool:
raise NotImplementedError

def read(self, key: str) -> Any:
raise NotImplementedError

def delete(self, *keys: str) -> int:
raise NotImplementedError

def save(self, key: str, object: Any):
raise NotImplementedError

Expand All @@ -33,6 +39,17 @@ def read(self, key: str) -> Any:
except Exception as e:
raise StorageError(f"InMemory load failed: {e}")

def delete(self, *keys: str) -> int:
try:
deleted_count = 0
for key in keys:
if key in self._storage:
del self._storage[key]
deleted_count += 1
return deleted_count
except Exception as e:
raise StorageError(f"InMemory delete failed: {e}")

def save(self, key: str, object: Any):
try:
object_str = json.dumps(object)
Expand All @@ -43,23 +60,29 @@ def save(self, key: str, object: Any):

class RedisStorage(Storage):
def __init__(self, host, port):
self._redis = redis.Redis(host=host, port=port, db=0)
self.redis = redis.Redis(host=host, port=port, db=0)

def exists(self, key: str) -> bool:
return self._redis.exists(key)
return self.redis.exists(key)

def read(self, key: str) -> Any:
try:
result_bytes = self._redis.get(key)
result_bytes = self.redis.get(key)
result_str = result_bytes.decode("utf-8")
result_dict = json.loads(result_str)
return result_dict
except Exception as e:
raise StorageError(f"Redis load failed: {e}")

def delete(self, *keys: str) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally optional but how do you feel about adding some unit testing for the new function?

try:
return self.redis.delete(*keys)
except Exception as e:
raise StorageError(f"Redis delete failed: {e}")

def save(self, key: str, object: Any):
try:
object_str = json.dumps(object)
self._redis.set(key, object_str)
self.redis.set(key, object_str)
except Exception as e:
raise StorageError(f"Redis save failed: {e}")