From cfb49b3f31fa925b4d0215cf597f7f1141999c03 Mon Sep 17 00:00:00 2001 From: nazanin Date: Sat, 22 Jul 2023 15:47:44 +0330 Subject: [PATCH 1/3] Add sorted set functions to RedisCache --- django_redis/cache.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/django_redis/cache.py b/django_redis/cache.py index 0b4d5890..27467352 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -184,3 +184,51 @@ 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 zremrangebyscore(self, *args, **kwargs): + return self.client.zremrangebyscore(*args, **kwargs) + + @omit_exception + def zrevrangebyscore(self, *args, **kwargs): + return self.client.zrevrangebyscore(*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) From b875112f0b200f2ebdba828265275dd55d41c784 Mon Sep 17 00:00:00 2001 From: nazanin Date: Sat, 22 Jul 2023 16:12:01 +0330 Subject: [PATCH 2/3] Add sorted set functions to DefaultClient and update RedisCache functions --- django_redis/cache.py | 28 ++- django_redis/client/default.py | 417 ++++++++++++++++++++++++--------- 2 files changed, 335 insertions(+), 110 deletions(-) diff --git a/django_redis/cache.py b/django_redis/cache.py index 27467352..1ef68d11 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -205,14 +205,14 @@ def zrevrange(self, *args, **kwargs): def zrangebyscore(self, *args, **kwargs): return self.client.zrangebyscore(*args, **kwargs) - @omit_exception - def zremrangebyscore(self, *args, **kwargs): - return self.client.zremrangebyscore(*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) @@ -232,3 +232,23 @@ def zlexcount(self, *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) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 6886b46b..70098151 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -65,7 +65,7 @@ def __contains__(self, key: Any) -> bool: return self.has_key(key) def get_next_client_index( - self, write: bool = True, tried: Optional[List[int]] = None + self, write: bool = True, tried: Optional[List[int]] = None ) -> int: """ Return a next index for read client. This function implements a default @@ -87,10 +87,10 @@ def get_next_client_index( return random.randint(1, len(self._server) - 1) def get_client( - self, - write: bool = True, - tried: Optional[List[int]] = None, - show_index: bool = False, + self, + write: bool = True, + tried: Optional[List[int]] = None, + show_index: bool = False, ): """ Method used for obtain a raw redis client. @@ -124,14 +124,14 @@ def disconnect(self, index=0, client=None): return self.connection_factory.disconnect(client) if client else None def set( - self, - key: Any, - value: Any, - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, - nx: bool = False, - xx: bool = False, + self, + key: Any, + value: Any, + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, + nx: bool = False, + xx: bool = False, ) -> bool: """ Persist a value to the cache, and set an optional expiration time. @@ -175,9 +175,9 @@ def set( return bool(client.set(nkey, nvalue, nx=nx, px=timeout, xx=xx)) except _main_exceptions as e: if ( - not original_client - and not self._replica_read_only - and len(tried) < len(self._server) + not original_client + and not self._replica_read_only + and len(tried) < len(self._server) ): tried.append(index) client = None @@ -185,11 +185,11 @@ def set( raise ConnectionInterrupted(connection=client) from e def incr_version( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> int: """ Adds delta to the cache version for the supplied key. Returns the @@ -223,12 +223,12 @@ def incr_version( return version + delta def add( - self, - key: Any, - value: Any, - timeout: Any = DEFAULT_TIMEOUT, - version: Optional[Any] = None, - client: Optional[Redis] = None, + self, + key: Any, + value: Any, + timeout: Any = DEFAULT_TIMEOUT, + version: Optional[Any] = None, + client: Optional[Redis] = None, ) -> bool: """ Add a value to the cache, failing if the key already exists. @@ -238,11 +238,11 @@ def add( return self.set(key, value, timeout, version=version, client=client, nx=True) def get( - self, - key: Any, - default=None, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> Any: """ Retrieve a value from the cache. @@ -265,7 +265,8 @@ def get( return self.decode(value) def persist( - self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, + client: Optional[Redis] = None ) -> bool: if client is None: client = self.get_client(write=True) @@ -275,11 +276,11 @@ def persist( return client.persist(key) def expire( - self, - key: Any, - timeout, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + timeout, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: if client is None: client = self.get_client(write=True) @@ -299,11 +300,11 @@ def pexpire(self, key, timeout, version=None, client=None) -> bool: return bool(client.pexpire(key, timeout)) def pexpire_at( - self, - key: Any, - when: Union[datetime, int], - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + when: Union[datetime, int], + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Set an expire flag on a ``key`` to ``when``, which can be represented @@ -317,11 +318,11 @@ def pexpire_at( return bool(client.pexpireat(key, when)) def expire_at( - self, - key: Any, - when: Union[datetime, int], - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + when: Union[datetime, int], + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Set an expire flag on a ``key`` to ``when``, which can be represented @@ -335,14 +336,14 @@ def expire_at( return client.expireat(key, when) def lock( - self, - key, - version: Optional[int] = None, - timeout=None, - sleep=0.1, - blocking_timeout=None, - client: Optional[Redis] = None, - thread_local=True, + self, + key, + version: Optional[int] = None, + timeout=None, + sleep=0.1, + blocking_timeout=None, + client: Optional[Redis] = None, + thread_local=True, ): if client is None: client = self.get_client(write=True) @@ -357,11 +358,11 @@ def lock( ) def delete( - self, - key: Any, - version: Optional[int] = None, - prefix: Optional[str] = None, - client: Optional[Redis] = None, + self, + key: Any, + version: Optional[int] = None, + prefix: Optional[str] = None, + client: Optional[Redis] = None, ) -> int: """ Remove a key from the cache. @@ -375,12 +376,12 @@ def delete( raise ConnectionInterrupted(connection=client) from e def delete_pattern( - self, - pattern: str, - version: Optional[int] = None, - prefix: Optional[str] = None, - client: Optional[Redis] = None, - itersize: Optional[int] = None, + self, + pattern: str, + version: Optional[int] = None, + prefix: Optional[str] = None, + client: Optional[Redis] = None, + itersize: Optional[int] = None, ) -> int: """ Remove all keys matching pattern. @@ -405,7 +406,7 @@ def delete_pattern( raise ConnectionInterrupted(connection=client) from e def delete_many( - self, keys, version: Optional[int] = None, client: Optional[Redis] = None + self, keys, version: Optional[int] = None, client: Optional[Redis] = None ): """ Remove multiple keys at once. @@ -465,7 +466,7 @@ def encode(self, value: Any) -> Union[bytes, Any]: return value def get_many( - self, keys, version: Optional[int] = None, client: Optional[Redis] = None + self, keys, version: Optional[int] = None, client: Optional[Redis] = None ) -> OrderedDict: """ Retrieve many keys. @@ -493,11 +494,11 @@ def get_many( return recovered_data def set_many( - self, - data: Dict[Any, Any], - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + data: Dict[Any, Any], + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> None: """ Set a bunch of values in the cache at once from a dict of key/value @@ -518,12 +519,12 @@ def set_many( raise ConnectionInterrupted(connection=client) from e def _incr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, - ignore_key_check: bool = False, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, + ignore_key_check: bool = False, ) -> int: if client is None: client = self.get_client(write=True) @@ -570,12 +571,12 @@ def _incr( return value def incr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, - ignore_key_check: bool = False, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, + ignore_key_check: bool = False, ) -> int: """ Add delta to value in the cache. If the key does not exist, raise a @@ -591,11 +592,11 @@ def incr( ) def decr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> int: """ Decreace delta to value in the cache. If the key does not exist, raise a @@ -604,7 +605,8 @@ def decr( return self._incr(key=key, delta=-delta, version=version, client=client) def ttl( - self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, + client: Optional[Redis] = None ) -> Optional[int]: """ Executes TTL redis command and return the "time-to-live" of specified key. @@ -654,7 +656,8 @@ def pttl(self, key, version=None, client=None): return None def has_key( - self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, + client: Optional[Redis] = None ) -> bool: """ Test if key exists. @@ -670,11 +673,11 @@ def has_key( raise ConnectionInterrupted(connection=client) from e def iter_keys( - self, - search: str, - itersize: Optional[int] = None, - client: Optional[Redis] = None, - version: Optional[int] = None, + self, + search: str, + itersize: Optional[int] = None, + client: Optional[Redis] = None, + version: Optional[int] = None, ) -> Iterator[str]: """ Same as keys, but uses redis >= 2.8 cursors @@ -689,7 +692,8 @@ def iter_keys( yield self.reverse_key(item.decode()) def keys( - self, search: str, version: Optional[int] = None, client: Optional[Redis] = None + self, search: str, version: Optional[int] = None, + client: Optional[Redis] = None ) -> List[Any]: """ Execute KEYS command and return matched results. @@ -708,7 +712,7 @@ def keys( raise ConnectionInterrupted(connection=client) from e def make_key( - self, key: Any, version: Optional[Any] = None, prefix: Optional[str] = None + self, key: Any, version: Optional[Any] = None, prefix: Optional[str] = None ) -> CacheKey: if isinstance(key, CacheKey): return key @@ -722,7 +726,8 @@ def make_key( return CacheKey(self._backend.key_func(key, prefix, version)) def make_pattern( - self, pattern: str, version: Optional[int] = None, prefix: Optional[str] = None + self, pattern: str, version: Optional[int] = None, + prefix: Optional[str] = None ) -> CacheKey: if isinstance(pattern, CacheKey): return pattern @@ -753,11 +758,11 @@ def do_close_clients(self): self._clients = [None] * num_clients def touch( - self, - key: Any, - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Sets a new expiration for a key. @@ -776,3 +781,203 @@ 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) From d30315901415e4963bd054a55ef96ca1c9626135 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Jul 2023 11:15:00 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 281 ++++++++++++++++++--------------- 1 file changed, 154 insertions(+), 127 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 70098151..fed94653 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -65,7 +65,7 @@ def __contains__(self, key: Any) -> bool: return self.has_key(key) def get_next_client_index( - self, write: bool = True, tried: Optional[List[int]] = None + self, write: bool = True, tried: Optional[List[int]] = None ) -> int: """ Return a next index for read client. This function implements a default @@ -87,10 +87,10 @@ def get_next_client_index( return random.randint(1, len(self._server) - 1) def get_client( - self, - write: bool = True, - tried: Optional[List[int]] = None, - show_index: bool = False, + self, + write: bool = True, + tried: Optional[List[int]] = None, + show_index: bool = False, ): """ Method used for obtain a raw redis client. @@ -124,14 +124,14 @@ def disconnect(self, index=0, client=None): return self.connection_factory.disconnect(client) if client else None def set( - self, - key: Any, - value: Any, - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, - nx: bool = False, - xx: bool = False, + self, + key: Any, + value: Any, + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, + nx: bool = False, + xx: bool = False, ) -> bool: """ Persist a value to the cache, and set an optional expiration time. @@ -175,9 +175,9 @@ def set( return bool(client.set(nkey, nvalue, nx=nx, px=timeout, xx=xx)) except _main_exceptions as e: if ( - not original_client - and not self._replica_read_only - and len(tried) < len(self._server) + not original_client + and not self._replica_read_only + and len(tried) < len(self._server) ): tried.append(index) client = None @@ -185,11 +185,11 @@ def set( raise ConnectionInterrupted(connection=client) from e def incr_version( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> int: """ Adds delta to the cache version for the supplied key. Returns the @@ -223,12 +223,12 @@ def incr_version( return version + delta def add( - self, - key: Any, - value: Any, - timeout: Any = DEFAULT_TIMEOUT, - version: Optional[Any] = None, - client: Optional[Redis] = None, + self, + key: Any, + value: Any, + timeout: Any = DEFAULT_TIMEOUT, + version: Optional[Any] = None, + client: Optional[Redis] = None, ) -> bool: """ Add a value to the cache, failing if the key already exists. @@ -238,11 +238,11 @@ def add( return self.set(key, value, timeout, version=version, client=client, nx=True) def get( - self, - key: Any, - default=None, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> Any: """ Retrieve a value from the cache. @@ -265,8 +265,7 @@ def get( return self.decode(value) def persist( - self, key: Any, version: Optional[int] = None, - client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None ) -> bool: if client is None: client = self.get_client(write=True) @@ -276,11 +275,11 @@ def persist( return client.persist(key) def expire( - self, - key: Any, - timeout, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + timeout, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: if client is None: client = self.get_client(write=True) @@ -300,11 +299,11 @@ def pexpire(self, key, timeout, version=None, client=None) -> bool: return bool(client.pexpire(key, timeout)) def pexpire_at( - self, - key: Any, - when: Union[datetime, int], - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + when: Union[datetime, int], + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Set an expire flag on a ``key`` to ``when``, which can be represented @@ -318,11 +317,11 @@ def pexpire_at( return bool(client.pexpireat(key, when)) def expire_at( - self, - key: Any, - when: Union[datetime, int], - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + when: Union[datetime, int], + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Set an expire flag on a ``key`` to ``when``, which can be represented @@ -336,14 +335,14 @@ def expire_at( return client.expireat(key, when) def lock( - self, - key, - version: Optional[int] = None, - timeout=None, - sleep=0.1, - blocking_timeout=None, - client: Optional[Redis] = None, - thread_local=True, + self, + key, + version: Optional[int] = None, + timeout=None, + sleep=0.1, + blocking_timeout=None, + client: Optional[Redis] = None, + thread_local=True, ): if client is None: client = self.get_client(write=True) @@ -358,11 +357,11 @@ def lock( ) def delete( - self, - key: Any, - version: Optional[int] = None, - prefix: Optional[str] = None, - client: Optional[Redis] = None, + self, + key: Any, + version: Optional[int] = None, + prefix: Optional[str] = None, + client: Optional[Redis] = None, ) -> int: """ Remove a key from the cache. @@ -376,12 +375,12 @@ def delete( raise ConnectionInterrupted(connection=client) from e def delete_pattern( - self, - pattern: str, - version: Optional[int] = None, - prefix: Optional[str] = None, - client: Optional[Redis] = None, - itersize: Optional[int] = None, + self, + pattern: str, + version: Optional[int] = None, + prefix: Optional[str] = None, + client: Optional[Redis] = None, + itersize: Optional[int] = None, ) -> int: """ Remove all keys matching pattern. @@ -406,7 +405,7 @@ def delete_pattern( raise ConnectionInterrupted(connection=client) from e def delete_many( - self, keys, version: Optional[int] = None, client: Optional[Redis] = None + self, keys, version: Optional[int] = None, client: Optional[Redis] = None ): """ Remove multiple keys at once. @@ -466,7 +465,7 @@ def encode(self, value: Any) -> Union[bytes, Any]: return value def get_many( - self, keys, version: Optional[int] = None, client: Optional[Redis] = None + self, keys, version: Optional[int] = None, client: Optional[Redis] = None ) -> OrderedDict: """ Retrieve many keys. @@ -494,11 +493,11 @@ def get_many( return recovered_data def set_many( - self, - data: Dict[Any, Any], - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + data: Dict[Any, Any], + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> None: """ Set a bunch of values in the cache at once from a dict of key/value @@ -519,12 +518,12 @@ def set_many( raise ConnectionInterrupted(connection=client) from e def _incr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, - ignore_key_check: bool = False, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, + ignore_key_check: bool = False, ) -> int: if client is None: client = self.get_client(write=True) @@ -571,12 +570,12 @@ def _incr( return value def incr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, - ignore_key_check: bool = False, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, + ignore_key_check: bool = False, ) -> int: """ Add delta to value in the cache. If the key does not exist, raise a @@ -592,11 +591,11 @@ def incr( ) def decr( - self, - key: Any, - delta: int = 1, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + delta: int = 1, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> int: """ Decreace delta to value in the cache. If the key does not exist, raise a @@ -605,8 +604,7 @@ def decr( return self._incr(key=key, delta=-delta, version=version, client=client) def ttl( - self, key: Any, version: Optional[int] = None, - client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None ) -> Optional[int]: """ Executes TTL redis command and return the "time-to-live" of specified key. @@ -656,8 +654,7 @@ def pttl(self, key, version=None, client=None): return None def has_key( - self, key: Any, version: Optional[int] = None, - client: Optional[Redis] = None + self, key: Any, version: Optional[int] = None, client: Optional[Redis] = None ) -> bool: """ Test if key exists. @@ -673,11 +670,11 @@ def has_key( raise ConnectionInterrupted(connection=client) from e def iter_keys( - self, - search: str, - itersize: Optional[int] = None, - client: Optional[Redis] = None, - version: Optional[int] = None, + self, + search: str, + itersize: Optional[int] = None, + client: Optional[Redis] = None, + version: Optional[int] = None, ) -> Iterator[str]: """ Same as keys, but uses redis >= 2.8 cursors @@ -692,8 +689,7 @@ def iter_keys( yield self.reverse_key(item.decode()) def keys( - self, search: str, version: Optional[int] = None, - client: Optional[Redis] = None + self, search: str, version: Optional[int] = None, client: Optional[Redis] = None ) -> List[Any]: """ Execute KEYS command and return matched results. @@ -712,7 +708,7 @@ def keys( raise ConnectionInterrupted(connection=client) from e def make_key( - self, key: Any, version: Optional[Any] = None, prefix: Optional[str] = None + self, key: Any, version: Optional[Any] = None, prefix: Optional[str] = None ) -> CacheKey: if isinstance(key, CacheKey): return key @@ -726,8 +722,7 @@ def make_key( return CacheKey(self._backend.key_func(key, prefix, version)) def make_pattern( - self, pattern: str, version: Optional[int] = None, - prefix: Optional[str] = None + self, pattern: str, version: Optional[int] = None, prefix: Optional[str] = None ) -> CacheKey: if isinstance(pattern, CacheKey): return pattern @@ -758,11 +753,11 @@ def do_close_clients(self): self._clients = [None] * num_clients def touch( - self, - key: Any, - timeout: Optional[float] = DEFAULT_TIMEOUT, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + key: Any, + timeout: Optional[float] = DEFAULT_TIMEOUT, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> bool: """ Sets a new expiration for a key. @@ -815,8 +810,9 @@ def zrange(self, key: Any, start: int, stop: int, client: Optional[Redis] = None key = self.make_key(key) return client.zrange(key, start, stop) - def zrevrange(self, key: Any, start: int, stop: int, - client: Optional[Redis] = None): + 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. @@ -828,8 +824,15 @@ def zrevrange(self, key: Any, start: int, stop: int, 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): + 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. """ @@ -840,8 +843,15 @@ def zrangebyscore(self, key: Any, min: float, max: float, start: int, num: int, 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): + 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. @@ -853,8 +863,9 @@ def zrevrangebyscore(self, key: Any, max: float, min: float, start: int, num: in 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): + def zremrangebyscore( + self, key: Any, min: float, max: float, client: Optional[Redis] = None + ): """ Remove all members in a sorted set within the given scores. """ @@ -865,8 +876,15 @@ def zremrangebyscore(self, key: Any, min: float, max: float, 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): + 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. """ @@ -877,8 +895,15 @@ def zrangebylex(self, key: Any, min: str, max: str, start: int, num: int, 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): + 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. @@ -890,8 +915,9 @@ def zrevrangebylex(self, key: Any, max: str, min: str, start: int, num: int, 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): + 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. """ @@ -937,8 +963,9 @@ def zrevrank(self, key: Any, value: Any, client: Optional[Redis] = None): key = self.make_key(key) return client.zrevrank(key, value) - def zremrangebyrank(self, key: Any, start: int, stop: int, - client: Optional[Redis] = None): + def zremrangebyrank( + self, key: Any, start: int, stop: int, client: Optional[Redis] = None + ): """ Remove all members in a sorted set within the given indexes. """