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):

Copy link
Contributor Author

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. :)

Copy link
Contributor Author

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.

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

Copy link
Contributor Author

@Kariton Kariton Feb 12, 2025

Choose a reason for hiding this comment

The 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?
IIRC without that i was unable to determine what what kind of storage is used.
i need to know if the storage is persistent because it is mandatory for the command.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}")