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

Add missing methods #721

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.d/721.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduction of sets and support of basic operations.
72 changes: 72 additions & 0 deletions django_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,75 @@ def hkeys(self, *args, **kwargs):
@omit_exception
def hexists(self, *args, **kwargs):
return self.client.hexists(*args, **kwargs)

@omit_exception
def sadd(self, *args, **kwargs):
return self.client.sadd(*args, **kwargs)

@omit_exception
def scard(self, *args, **kwargs):
return self.client.scard(*args, **kwargs)

@omit_exception
def sdiff(self, *args, **kwargs):
return self.client.sdiff(*args, **kwargs)

@omit_exception
def sdiffstore(self, *args, **kwargs):
return self.client.sdiffstore(*args, **kwargs)

@omit_exception
def sinter(self, *args, **kwargs):
return self.client.sinter(*args, **kwargs)

@omit_exception
def sinterstore(self, *args, **kwargs):
return self.client.sinterstore(*args, **kwargs)

@omit_exception
def sismember(self, *args, **kwargs):
return self.client.sismember(*args, **kwargs)

@omit_exception
def smismember(self, *args, **kwargs):
return self.client.smismember(*args, **kwargs)

@omit_exception
def smembers(self, *args, **kwargs):
return self.client.smembers(*args, **kwargs)

@omit_exception
def smove(self, *args, **kwargs):
return self.client.smove(*args, **kwargs)

@omit_exception
def spop(self, *args, **kwargs):
return self.client.spop(*args, **kwargs)

@omit_exception
def srandmember(self, *args, **kwargs):
return self.client.srandmember(*args, **kwargs)

@omit_exception
def srem(self, *args, **kwargs):
return self.client.srem(*args, **kwargs)

@omit_exception
def sunion(self, *args, **kwargs):
return self.client.sunion(*args, **kwargs)

@omit_exception
def sunionstore(self, *args, **kwargs):
return self.client.sunionstore(*args, **kwargs)

@omit_exception
def sintercard(self, *args, **kwargs):
return self.client.sintercard(*args, **kwargs)

@omit_exception
def smismember(self, *args, **kwargs):
return self.client.smismember(*args, **kwargs)

@omit_exception
def sscan(self, *args, **kwargs):
return self.client.sscan(*args, **kwargs)
264 changes: 263 additions & 1 deletion django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
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]:
if client is None:
client = self.get_client(write=True)

key = self.make_key(key, version=version)
result = client.spop(key, count)
if isinstance(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]:
if client is None:
client = self.get_client(write=False)

key = self.make_key(key, version=version)
result = client.srandmember(key, count)
if isinstance(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:
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]:
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
Loading
Loading