-
-
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 missing methods #721
base: master
Are you sure you want to change the base?
Add missing methods #721
Changes from 10 commits
a5edf9d
ad67b39
2b84e94
ff2adf0
8eabde4
3d0c420
44803d0
403d503
f567f0e
e4c7cb5
ac00f69
0a0a087
691edd1
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 @@ | ||
Introduction of sets and support of basic operations. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import socket | ||
from collections import OrderedDict | ||
from contextlib import suppress | ||
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union | ||
from typing import Any, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Union | ||
|
||
from django.conf import settings | ||
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache, get_key_func | ||
|
@@ -889,3 +889,265 @@ def hexists( | |
client = self.get_client(write=False) | ||
nkey = self.make_key(key, version=version) | ||
return bool(client.hexists(name, nkey)) | ||
|
||
def sadd( | ||
self, | ||
key: Any, | ||
*values: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
key = self.make_key(key, version=version) | ||
values = [self.encode(value) for value in values] | ||
return int(client.sadd(key, *values)) | ||
|
||
def scard( | ||
self, | ||
key: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
return int(client.scard(key)) | ||
|
||
def sdiff( | ||
self, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> set: | ||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
keys = [self.make_key(key, version=version) for key in keys] | ||
return {self.decode(value) for value in client.sdiff(*keys)} | ||
|
||
def sdiffstore( | ||
self, | ||
dest: Any, | ||
key: Any, | ||
*keys, | ||
version_dest: Optional[int] = None, | ||
version_minuend: Optional[int] = None, | ||
version_subtrahend: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
dest = self.make_key(dest, version=version_dest) | ||
minuend_key = self.make_key(key, version=version_minuend) | ||
subtrahend_keys: Set[str] = { | ||
self.make_key(key_, version=version_subtrahend) for key_ in keys | ||
} | ||
return int(client.sdiffstore(dest, minuend_key, *subtrahend_keys)) | ||
|
||
def sinter( | ||
self, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> set: | ||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
keys = [self.make_key(key, version=version) for key in keys] | ||
return {self.decode(value) for value in client.sinter(*keys)} | ||
|
||
def sinterstore( | ||
self, | ||
dest: Any, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
dest = self.make_key(dest, version=version) | ||
keys = [self.make_key(key, version=version) for key in keys] | ||
return int(client.sinterstore(dest, *keys)) | ||
|
||
def sismember( | ||
self, | ||
key: Any, | ||
member: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> bool: | ||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
member = self.encode(member) | ||
return bool(client.sismember(key, member)) | ||
|
||
def smembers( | ||
self, | ||
key: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> set: | ||
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. you need to change this one to maybe it's not always a set, have you checked |
||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
return {self.decode(value) for value in client.smembers(key)} | ||
|
||
def smove( | ||
self, | ||
source: Any, | ||
destination: Any, | ||
member: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> bool: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
source = self.make_key(source, version=version) | ||
destination = self.make_key(destination) | ||
member = self.encode(member) | ||
return bool(client.smove(source, destination, member)) | ||
|
||
def spop( | ||
self, | ||
key: Any, | ||
count: Optional[int] = None, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> Union[set, Any]: | ||
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. you need to change this one to maybe it's not always a set, have you checked |
||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
key = self.make_key(key, version=version) | ||
result = client.spop(key, count) | ||
if type(result) == list: | ||
return {self.decode(value) for value in result} | ||
return self.decode(result) | ||
|
||
def srandmember( | ||
self, | ||
key: Any, | ||
count: Optional[int] = None, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> Union[set, Any]: | ||
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. you need to change this one to maybe it's not always a set, have you checked |
||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
result = client.srandmember(key, count) | ||
if type(result) == list: | ||
return {self.decode(value) for value in result} | ||
return self.decode(result) | ||
|
||
def srem( | ||
self, | ||
key: Any, | ||
*members, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
key = self.make_key(key, version=version) | ||
members = [self.decode(member) for member in members] | ||
return int(client.srem(key, *members)) | ||
|
||
def sunion( | ||
self, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> set: | ||
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. you need to change this one to |
||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
keys = [self.make_key(key, version=version) for key in keys] | ||
return {self.decode(value) for value in client.sunion(*keys)} | ||
|
||
def sunionstore( | ||
self, | ||
destination: Any, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
destination = self.make_key(destination, version=version) | ||
keys = [self.make_key(key, version=version) for key in keys] | ||
return int(client.sunionstore(destination, *keys)) | ||
|
||
def sintercard( | ||
self, | ||
*keys, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> int: | ||
if client is None: | ||
client = self.get_client(write=True) | ||
|
||
keys = [self.make_key(key, version=version) for key in keys] | ||
|
||
result_key = "__temp_inter_key__" | ||
client.sinterstore(result_key, *keys) | ||
cardinality = client.scard(result_key) | ||
client.delete(result_key) | ||
|
||
return cardinality | ||
|
||
def smismember( | ||
self, | ||
key: Any, | ||
members: Any, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> bool: | ||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
members = [self.encode(member) for member in members] | ||
|
||
with client.pipeline() as pipe: | ||
for member in members: | ||
pipe.sismember(key, member) | ||
|
||
results = pipe.execute() | ||
|
||
return all(bool(result) for result in results) | ||
|
||
def sscan( | ||
self, | ||
key: Any, | ||
cursor: int = 0, | ||
match: Optional[str] = None, | ||
count: Optional[int] = None, | ||
version: Optional[int] = None, | ||
client: Optional[Redis] = None, | ||
) -> set[Any]: | ||
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. you need to change this one to |
||
if client is None: | ||
client = self.get_client(write=False) | ||
|
||
key = self.make_key(key, version=version) | ||
elements = set() | ||
|
||
while True: | ||
result = client.sscan(key, cursor, match=match, count=count) | ||
cursor, partial_elements = result | ||
elements.update(self.decode(value) for value in partial_elements) | ||
|
||
if cursor == 0: | ||
break | ||
|
||
return elements |
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.
you need to change this one to
Set