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 sorted set functions to RedisCache #673

Open
wants to merge 3 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
68 changes: 68 additions & 0 deletions django_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,71 @@ def close(self, **kwargs):
@omit_exception
def touch(self, *args, **kwargs):
return self.client.touch(*args, **kwargs)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@omit_exception
def zcount(self, *args, **kwargs):
return self.client.zcount(*args, **kwargs)
232 changes: 232 additions & 0 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,235 @@ def touch(
# Convert to milliseconds
timeout = int(timeout * 1000)
return bool(client.pexpire(key, timeout))

def zadd(self, key: Any, value: Any, score: float, client: Optional[Redis] = None):
"""
Add a new member to a sorted set.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_key(key)
return client.zadd(key, {value: score})

def zrem(self, key: Any, value: Any, client: Optional[Redis] = None):
"""
Remove a member from a sorted set.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_key(key)
return client.zrem(key, value)

def zrange(self, key: Any, start: int, stop: int, client: Optional[Redis] = None):
"""
Return a range of members in a sorted set, by index.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrange(key, start, stop)

def zrevrange(
self, key: Any, start: int, stop: int, client: Optional[Redis] = None
):
"""
Return a range of members in a sorted set,
by index, with scores ordered from high to low.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrevrange(key, start, stop)

def zrangebyscore(
self,
key: Any,
min: float,
max: float,
start: int,
num: int,
client: Optional[Redis] = None,
):
"""
Return a range of members in a sorted set, by score.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrangebyscore(key, min, max, start, num)

def zrevrangebyscore(
self,
key: Any,
max: float,
min: float,
start: int,
num: int,
client: Optional[Redis] = None,
):
"""
Return a range of members in a sorted set,
by score, with scores ordered from high to low.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrevrangebyscore(key, max, min, start, num)

def zremrangebyscore(
self, key: Any, min: float, max: float, client: Optional[Redis] = None
):
"""
Remove all members in a sorted set within the given scores.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_key(key)
return client.zremrangebyscore(key, min, max)

def zrangebylex(
self,
key: Any,
min: str,
max: str,
start: int,
num: int,
client: Optional[Redis] = None,
):
"""
Return a range of members in a sorted set, by lexicographical range.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrangebylex(key, min, max, start, num)

def zrevrangebylex(
self,
key: Any,
max: str,
min: str,
start: int,
num: int,
client: Optional[Redis] = None,
):
"""
Return a range of members in a sorted set,
by lexicographical range, ordered from higher to lower strings.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrevrangebylex(key, max, min, start, num)

def zremrangebylex(
self, key: Any, min: str, max: str, client: Optional[Redis] = None
):
"""
Remove all members in a sorted set between the given lexicographical range.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_key(key)
return client.zremrangebylex(key, min, max)

def zlexcount(self, key: Any, min: str, max: str, client: Optional[Redis] = None):
"""
Count the number of members in a sorted set
between a given lexicographical range.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zlexcount(key, min, max)

def zrank(self, key: Any, value: Any, client: Optional[Redis] = None):
"""
Determine the index of a member in a sorted set.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrank(key, value)

def zrevrank(self, key: Any, value: Any, client: Optional[Redis] = None):
"""
Determine the index of a member in a sorted set,
with scores ordered from high to low.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zrevrank(key, value)

def zremrangebyrank(
self, key: Any, start: int, stop: int, client: Optional[Redis] = None
):
"""
Remove all members in a sorted set within the given indexes.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_key(key)
return client.zremrangebyrank(key, start, stop)

def zscore(self, key: Any, value: Any, client: Optional[Redis] = None):
"""
Get the score associated with the given member in a sorted set.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zscore(key, value)

def zcard(self, key: Any, client: Optional[Redis] = None):
"""
Get the number of members in a sorted set.
"""

if client is None:
client = self.get_client(write=False)

key = self.make_key(key)
return client.zcard(key)

def zcount(self, key: Any, min: float, max: float, client: Optional[Redis] = None):
"""
Count the members in a sorted set with scores within the given values.
"""

if client is None:
client = self.get_client(write=True)

key = self.make_kwey(key)
return client.zcount(key, min, max)