-
Notifications
You must be signed in to change notification settings - Fork 31
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 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. class someCommand(Command):
async def handle(self, c: Context):
if c.bot.storage.redis == None:
logger.error("[someCommand] Redis storage backend required.")
return
try:
# stuff this is why i wrote it that way. maybe there is a better solution? 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. Then I don't think this is needed at all and you can simplify it by doing this instead: if not isinstance(c.bot.storage, RedisStorage):
logger.error("[someCommand] Redis storage backend required.") |
||
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? 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. agree, will see what i can do. |
||
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?
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.
thank you for taking the time and reviewing the PR.
after all i'm not a programmer and as script kiddy i dont know the implecations and "right ways" in many cases.
when this is the preferred / right way i won't say no. :)
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.
short glimpse at the python docs helped me to understand what you mean.
sounds like a great idea.
will look into it.