-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
Add support for hashmaps #719
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Support HashMaps |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -813,3 +813,79 @@ | |
# Convert to milliseconds | ||
timeout = int(timeout * 1000) | ||
return bool(client.pexpire(key, timeout)) | ||
|
||
def hset( | ||
self, | ||
name: str, | ||
key: KeyT, | ||
value: EncodableT, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
""" | ||
Set the value of hash name at key to value. | ||
Returns the number of fields added to the hash. | ||
""" | ||
if client is None: | ||
client = self.get_client(write=True) | ||
nkey = self.make_key(key, version=version) | ||
nvalue = self.encode(value) | ||
return int(client.hset(name, nkey, nvalue)) | ||
|
||
def hdel( | ||
self, | ||
name: str, | ||
key: KeyT, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
""" | ||
Remove keys from hash name. | ||
Returns the number of fields deleted from the hash. | ||
""" | ||
if client is None: | ||
client = self.get_client(write=True) | ||
nkey = self.make_key(key, version=version) | ||
return int(client.hdel(name, nkey)) | ||
|
||
def hlen( | ||
self, | ||
name: str, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
""" | ||
Return the number of items in hash name. | ||
""" | ||
if client is None: | ||
client = self.get_client(write=False) | ||
return int(client.hlen(name)) | ||
|
||
def hkeys( | ||
self, | ||
name: str, | ||
client: Optional[Redis] = None, | ||
) -> List: | ||
""" | ||
Return a list of keys in hash name. | ||
""" | ||
if client is None: | ||
client = self.get_client(write=False) | ||
try: | ||
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. why the try/catch? 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 implemented hkeys() similar to keys() function. If you think that the try/catch is redundant, I can remove it. 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. it's okay then, it was implemented way before I took this project, maybe we should discuss it in a separate time |
||
return [self.reverse_key(k.decode()) for k in client.hkeys(name)] | ||
except _main_exceptions as e: | ||
raise ConnectionInterrupted(connection=client) from e | ||
|
||
def hexists( | ||
self, | ||
name: str, | ||
key: KeyT, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> bool: | ||
""" | ||
Return True if key exists in hash name, else False. | ||
""" | ||
if client is None: | ||
client = self.get_client(write=False) | ||
nkey = self.make_key(key, version=version) | ||
return bool(client.hexists(name, nkey)) |
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.
list of what?
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.
Fixed it.