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

Check warning on line 190 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L190

Added line #L190 was not covered by tests

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

Check warning on line 194 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L194

Added line #L194 was not covered by tests

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

Check warning on line 198 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L198

Added line #L198 was not covered by tests

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

Check warning on line 202 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L202

Added line #L202 was not covered by tests

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

Check warning on line 206 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L206

Added line #L206 was not covered by tests

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

Check warning on line 210 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L210

Added line #L210 was not covered by tests

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

Check warning on line 214 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L214

Added line #L214 was not covered by tests

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

Check warning on line 218 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L218

Added line #L218 was not covered by tests

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

Check warning on line 222 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L222

Added line #L222 was not covered by tests

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

Check warning on line 226 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L226

Added line #L226 was not covered by tests

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

Check warning on line 230 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L230

Added line #L230 was not covered by tests

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

Check warning on line 234 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L234

Added line #L234 was not covered by tests

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

Check warning on line 238 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L238

Added line #L238 was not covered by tests

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

Check warning on line 242 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L242

Added line #L242 was not covered by tests

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

Check warning on line 246 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L246

Added line #L246 was not covered by tests

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

Check warning on line 250 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L250

Added line #L250 was not covered by tests

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

Check warning on line 254 in django_redis/cache.py

View check run for this annotation

Codecov / codecov/patch

django_redis/cache.py#L254

Added line #L254 was not covered by tests
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 @@
# 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)

Check warning on line 786 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L785-L786

Added lines #L785 - L786 were not covered by tests

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

Check warning on line 789 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L788-L789

Added lines #L788 - L789 were not covered by tests

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)

Check warning on line 797 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L796-L797

Added lines #L796 - L797 were not covered by tests

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

Check warning on line 800 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L799-L800

Added lines #L799 - L800 were not covered by tests

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)

Check warning on line 808 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L807-L808

Added lines #L807 - L808 were not covered by tests

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

Check warning on line 811 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L810-L811

Added lines #L810 - L811 were not covered by tests

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)

Check warning on line 822 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L821-L822

Added lines #L821 - L822 were not covered by tests

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

Check warning on line 825 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L824-L825

Added lines #L824 - L825 were not covered by tests

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)

Check warning on line 841 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L840-L841

Added lines #L840 - L841 were not covered by tests

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

Check warning on line 844 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L843-L844

Added lines #L843 - L844 were not covered by tests

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)

Check warning on line 861 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L860-L861

Added lines #L860 - L861 were not covered by tests

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

Check warning on line 864 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L863-L864

Added lines #L863 - L864 were not covered by tests

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)

Check warning on line 874 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L873-L874

Added lines #L873 - L874 were not covered by tests

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

Check warning on line 877 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L876-L877

Added lines #L876 - L877 were not covered by tests

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)

Check warning on line 893 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L892-L893

Added lines #L892 - L893 were not covered by tests

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

Check warning on line 896 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L895-L896

Added lines #L895 - L896 were not covered by tests

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)

Check warning on line 913 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L912-L913

Added lines #L912 - L913 were not covered by tests

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

Check warning on line 916 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L915-L916

Added lines #L915 - L916 were not covered by tests

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)

Check warning on line 926 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L925-L926

Added lines #L925 - L926 were not covered by tests

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

Check warning on line 929 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L928-L929

Added lines #L928 - L929 were not covered by tests

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)

Check warning on line 938 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L937-L938

Added lines #L937 - L938 were not covered by tests

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

Check warning on line 941 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L940-L941

Added lines #L940 - L941 were not covered by tests

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)

Check warning on line 949 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L948-L949

Added lines #L948 - L949 were not covered by tests

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

Check warning on line 952 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L951-L952

Added lines #L951 - L952 were not covered by tests

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)

Check warning on line 961 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L960-L961

Added lines #L960 - L961 were not covered by tests

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

Check warning on line 964 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L963-L964

Added lines #L963 - L964 were not covered by tests

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)

Check warning on line 974 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L973-L974

Added lines #L973 - L974 were not covered by tests

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

Check warning on line 977 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L976-L977

Added lines #L976 - L977 were not covered by tests

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)

Check warning on line 985 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L984-L985

Added lines #L984 - L985 were not covered by tests

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

Check warning on line 988 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L987-L988

Added lines #L987 - L988 were not covered by tests

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)

Check warning on line 996 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L995-L996

Added lines #L995 - L996 were not covered by tests

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

Check warning on line 999 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L998-L999

Added lines #L998 - L999 were not covered by tests

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)

Check warning on line 1007 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L1006-L1007

Added lines #L1006 - L1007 were not covered by tests

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

Check warning on line 1010 in django_redis/client/default.py

View check run for this annotation

Codecov / codecov/patch

django_redis/client/default.py#L1009-L1010

Added lines #L1009 - L1010 were not covered by tests
Loading