-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,18 @@ | |
|
||
|
||
class Storage: | ||
def __init__(self): | ||
self.redis = None | ||
|
||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why the base |
||
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 | ||
|
||
|
@@ -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) | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") |
There was a problem hiding this comment.
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?