From d5919c1dd66475c08eeeb16e82efa782f2074d7e Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 12:24:01 +0900 Subject: [PATCH 01/21] feat : add 'hset, hdel' interfaces --- django_redis/cache.py | 8 ++ django_redis/client/default.py | 255 +++++++++++++++++++-------------- 2 files changed, 157 insertions(+), 106 deletions(-) diff --git a/django_redis/cache.py b/django_redis/cache.py index d3e8708d..67f8ef69 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -183,3 +183,11 @@ def close(self, **kwargs): @omit_exception def touch(self, *args, **kwargs): return self.client.touch(*args, **kwargs) + + @omit_exception + def hdel(self, *args, **kwargs): + return self.client.hdel(*args, **kwargs) + + @omit_exception + def hset(self, *args, **kwargs): + return self.client.hset(*args, **kwargs) \ No newline at end of file diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 1df90a27..3f96bf90 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -772,3 +772,46 @@ def touch( # Convert to milliseconds timeout = int(timeout * 1000) return bool(client.pexpire(key, timeout)) + + def hdel(self, + name: str, + *keys: List, + client: Optional[Redis] = None, + ) -> bool: + + if client is None: + client = self.get_client(write=True) + try: + return client.hdel(name, *keys) + except _main_exceptions as e: + raise ConnectionInterrupted(connection=client) from e + + def hset( + self, + name: str, + key: Optional[str] = None, + value: Optional[str] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, + ) -> bool: + + original_client = client + tried = [] # type: List[int] + while True: + try: + + if client is None: + client, index = self.get_client( + write=True, tried=tried, show_index=True + ) + return bool(client.hset(name, key, value, mapping)) + except _main_exceptions as e: + if ( + not original_client + and not self._replica_read_only + and len(tried) < len(self._server) + ): + tried.append(index) + client = None + continue + raise ConnectionInterrupted(connection=client) from e From 3cd84e5b206f477e35568edd7a50454710f7c829 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 03:26:02 +0000 Subject: [PATCH 02/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/cache.py | 2 +- django_redis/client/default.py | 241 +++++++++++++++++---------------- 2 files changed, 122 insertions(+), 121 deletions(-) diff --git a/django_redis/cache.py b/django_redis/cache.py index 67f8ef69..e13bdfab 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -190,4 +190,4 @@ def hdel(self, *args, **kwargs): @omit_exception def hset(self, *args, **kwargs): - return self.client.hset(*args, **kwargs) \ No newline at end of file + return self.client.hset(*args, **kwargs) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 3f96bf90..078a8368 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -773,11 +773,12 @@ def touch( timeout = int(timeout * 1000) return bool(client.pexpire(key, timeout)) - def hdel(self, - name: str, - *keys: List, - client: Optional[Redis] = None, - ) -> bool: + def hdel( + self, + name: str, + *keys: List, + client: Optional[Redis] = None, + ) -> bool: if client is None: client = self.get_client(write=True) @@ -787,12 +788,12 @@ def hdel(self, raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: str, - key: Optional[str] = None, - value: Optional[str] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: str, + key: Optional[str] = None, + value: Optional[str] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -807,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From 8bae96c76cfc0f197fe2cdb3a2571a3cccb10f0d Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 13:31:16 +0900 Subject: [PATCH 03/21] feat : add 'hset, hdel' interfaces\nfix : type hints --- changelog.d/373 | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 changelog.d/373 diff --git a/changelog.d/373 b/changelog.d/373 new file mode 100644 index 00000000..e69de29b From 8fa089fe52fb6f1edb19dd7baf6086ff04b23969 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 13:33:22 +0900 Subject: [PATCH 04/21] fix - type hints add - changelog.d --- changelog.d/373 | 1 + django_redis/client/default.py | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/changelog.d/373 b/changelog.d/373 index e69de29b..0bde78ca 100644 --- a/changelog.d/373 +++ b/changelog.d/373 @@ -0,0 +1 @@ +The interface corresponding to hset, hdel of redis-py has been created. \ No newline at end of file diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 3f96bf90..d5e9f64e 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -774,7 +774,7 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel(self, - name: str, + name: Any, *keys: List, client: Optional[Redis] = None, ) -> bool: @@ -786,11 +786,12 @@ def hdel(self, except _main_exceptions as e: raise ConnectionInterrupted(connection=client) from e + def hset( self, - name: str, - key: Optional[str] = None, - value: Optional[str] = None, + name: Any, + key: Optional[Any] = None, + value: Optional[Any] = None, mapping: Optional[dict] = None, client: Optional[Redis] = None, ) -> bool: From fc2a9089caab1ac727da3e276be5a6a5355b2137 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 04:41:31 +0000 Subject: [PATCH 05/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 244 ++++++++++++++++----------------- 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 0ffd644d..848b6603 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -773,12 +773,13 @@ def touch( timeout = int(timeout * 1000) return bool(client.pexpire(key, timeout)) - def hdel(self, - name: Any, - *keys: List, - client: Optional[Redis] = None, - ) -> bool: - + def hdel( + self, + name: Any, + *keys: List, + client: Optional[Redis] = None, + ) -> bool: + if client is None: client = self.get_client(write=True) try: @@ -786,14 +787,13 @@ def hdel(self, except _main_exceptions as e: raise ConnectionInterrupted(connection=client) from e - def hset( - self, - name: Any, - key: Optional[Any] = None, - value: Optional[Any] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Optional[Any] = None, + value: Optional[Any] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -808,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From b8aa93219d9cb5dc1592d9c6feda103ebd23c542 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 14:06:24 +0900 Subject: [PATCH 06/21] fix - return type , changelog filename format --- changelog.d/{373 => 373.feat} | 0 django_redis/client/default.py | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename changelog.d/{373 => 373.feat} (100%) diff --git a/changelog.d/373 b/changelog.d/373.feat similarity index 100% rename from changelog.d/373 rename to changelog.d/373.feat diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 0ffd644d..0dd73b32 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -778,11 +778,11 @@ def hdel(self, *keys: List, client: Optional[Redis] = None, ) -> bool: - + if client is None: client = self.get_client(write=True) try: - return client.hdel(name, *keys) + return bool(client.hdel(name, *keys)) except _main_exceptions as e: raise ConnectionInterrupted(connection=client) from e From cd8200290de68479f41ea18fdf896d141e06d7ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 05:08:33 +0000 Subject: [PATCH 07/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 238 ++++++++++++++++----------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 39c990c7..bb6458c5 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,10 +774,10 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: List, - client: Optional[Redis] = None, + self, + name: Any, + *keys: List, + client: Optional[Redis] = None, ) -> bool: if client is None: @@ -788,12 +788,12 @@ def hdel( raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Optional[Any] = None, - value: Optional[Any] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Optional[Any] = None, + value: Optional[Any] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -808,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From ff99a258f8dcc0b0d3a6036eefb2cb5fc545172a Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 14:33:33 +0900 Subject: [PATCH 08/21] fix - return type , changelog filename format --- changelog.d/{373.feat => 373.feature} | 0 django_redis/client/default.py | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename changelog.d/{373.feat => 373.feature} (100%) diff --git a/changelog.d/373.feat b/changelog.d/373.feature similarity index 100% rename from changelog.d/373.feat rename to changelog.d/373.feature diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 39c990c7..7bd25cc1 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -776,7 +776,7 @@ def touch( def hdel( self, name: Any, - *keys: List, + *keys: Union[str, bytes], client: Optional[Redis] = None, ) -> bool: @@ -790,8 +790,8 @@ def hdel( def hset( self, name: Any, - key: Optional[Any] = None, - value: Optional[Any] = None, + key: Union[str, bytes] = None, + value: Union[bytes, float, int, str] = None, mapping: Optional[dict] = None, client: Optional[Redis] = None, ) -> bool: From df94dc5582386ef1efbcf7ac6aa118d9d02a596e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 05:34:43 +0000 Subject: [PATCH 09/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 238 ++++++++++++++++----------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 7bd25cc1..e45a60d8 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,10 +774,10 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, ) -> bool: if client is None: @@ -788,12 +788,12 @@ def hdel( raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Union[str, bytes] = None, - value: Union[bytes, float, int, str] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Union[str, bytes] = None, + value: Union[bytes, float, int, str] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -808,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From 7fe3947c00782db718e701ba6091035b362338b0 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 14:41:42 +0900 Subject: [PATCH 10/21] fix - type hint --- django_redis/client/default.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 7bd25cc1..7a8fd00c 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -790,8 +790,8 @@ def hdel( def hset( self, name: Any, - key: Union[str, bytes] = None, - value: Union[bytes, float, int, str] = None, + key: Union[str, bytes], + value: Union[bytes, float, int, str], mapping: Optional[dict] = None, client: Optional[Redis] = None, ) -> bool: From b368de033f025723869ea73334c3763b24a6dea4 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Thu, 11 Aug 2022 14:45:31 +0900 Subject: [PATCH 11/21] fix : lint --- django_redis/client/default.py | 226 ++++++++++++++++----------------- 1 file changed, 113 insertions(+), 113 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 3f47d505..7a8fd00c 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,10 +774,10 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, ) -> bool: if client is None: @@ -808,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From f01d2d4440b7305fc9729a19a2fa33445ad77fd0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 05:45:51 +0000 Subject: [PATCH 12/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 238 ++++++++++++++++----------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 7a8fd00c..83e6fbef 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,10 +774,10 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, ) -> bool: if client is None: @@ -788,12 +788,12 @@ def hdel( raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Union[str, bytes], - value: Union[bytes, float, int, str], - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Union[str, bytes], + value: Union[bytes, float, int, str], + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -808,9 +808,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 From 3e371094506e129028595cc3fbcd15913c2928be Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Tue, 13 Sep 2022 11:03:58 +0900 Subject: [PATCH 13/21] feat : add hget interfaces and write tests --- django_redis/cache.py | 4 ++++ django_redis/client/default.py | 32 ++++++++++++++++++++++++++++++++ tests/test_backend.py | 14 ++++++++++++++ 3 files changed, 50 insertions(+) mode change 100644 => 100755 tests/test_backend.py diff --git a/django_redis/cache.py b/django_redis/cache.py index e13bdfab..533b2cdb 100644 --- a/django_redis/cache.py +++ b/django_redis/cache.py @@ -191,3 +191,7 @@ def hdel(self, *args, **kwargs): @omit_exception def hset(self, *args, **kwargs): return self.client.hset(*args, **kwargs) + + @omit_exception + def hget(self, *args, **kwargs): + return self.client.hget(*args, **kwargs) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 7a8fd00c..a27d2d8b 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -778,11 +778,15 @@ def hdel( name: Any, *keys: Union[str, bytes], client: Optional[Redis] = None, + version: Optional[int] = None, + ) -> bool: + name = self.make_key(name, version=version) if client is None: client = self.get_client(write=True) try: + return bool(client.hdel(name, *keys)) except _main_exceptions as e: raise ConnectionInterrupted(connection=client) from e @@ -792,12 +796,15 @@ def hset( name: Any, key: Union[str, bytes], value: Union[bytes, float, int, str], + version: Optional[int] = None, mapping: Optional[dict] = None, client: Optional[Redis] = None, ) -> bool: original_client = client tried = [] # type: List[int] + name = self.make_key(name, version=version) + value = self.encode(value) while True: try: @@ -805,6 +812,8 @@ def hset( client, index = self.get_client( write=True, tried=tried, show_index=True ) + print(bool(client.hset(name, key, value, mapping))) + print(name,key,value,'set') return bool(client.hset(name, key, value, mapping)) except _main_exceptions as e: if ( @@ -816,3 +825,26 @@ def hset( client = None continue raise ConnectionInterrupted(connection=client) from e + + def hget( + self, + name: str, + key: str, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, + ) -> Any: + if client is None: + client = self.get_client(write=False) + + name = self.make_key(name, version=version) + print(name,key,'get') + try: + value = client.hget(name, key) + except _main_exceptions as e: + raise ConnectionInterrupted(connection=client) from e + + if value is None: + return default + + return self.decode(value) diff --git a/tests/test_backend.py b/tests/test_backend.py old mode 100644 new mode 100755 index 0e8e1fdf..d6fefd9c --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -771,3 +771,17 @@ def test_clear(self, cache: RedisCache): cache.clear() value_from_cache_after_clear = cache.get("foo") assert value_from_cache_after_clear is None + + def test_hset_hget(self, cache: RedisCache): + cache.hset("foo", "bar", "baz") + cache.hset("foo", "baz", "bar") + value_from_cache = cache.hget("foo", "bar") + assert value_from_cache == "baz" + value_from_cache = cache.hget("foo", "baz") + assert value_from_cache == "bar" + + def test_hdel(self, cache: RedisCache): + cache.hset("foo", "bar", "baz") + cache.hset("foo", "baz", "bar") + cache.hdel("foo", "bar") + assert cache.hget("foo", "bar") == None \ No newline at end of file From e6b301f24b204309634b35c1f19b498b9d30d6c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 02:07:00 +0000 Subject: [PATCH 14/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 40 +++++++++++++++++----------------- tests/test_backend.py | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 9968a4a6..f92c21b4 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -774,11 +774,11 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, - version: Optional[int] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, + version: Optional[int] = None, ) -> bool: name = self.make_key(name, version=version) @@ -791,13 +791,13 @@ def hdel( raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Union[str, bytes], - value: Union[bytes, float, int, str], - version: Optional[int] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Union[str, bytes], + value: Union[bytes, float, int, str], + version: Optional[int] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -812,7 +812,7 @@ def hset( write=True, tried=tried, show_index=True ) print(bool(client.hset(name, key, value, mapping))) - print(name,key,value,'set') + print(name, key, value, "set") return bool(client.hset(name, key, value, mapping)) except _main_exceptions as e: if ( @@ -826,18 +826,18 @@ def hset( raise ConnectionInterrupted(connection=client) from e def hget( - self, - name: str, - key: str, - default=None, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + name: str, + key: str, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> Any: if client is None: client = self.get_client(write=False) name = self.make_key(name, version=version) - print(name,key,'get') + print(name, key, "get") try: value = client.hget(name, key) except _main_exceptions as e: diff --git a/tests/test_backend.py b/tests/test_backend.py index d6fefd9c..a77b5a09 100755 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -784,4 +784,4 @@ def test_hdel(self, cache: RedisCache): cache.hset("foo", "bar", "baz") cache.hset("foo", "baz", "bar") cache.hdel("foo", "bar") - assert cache.hget("foo", "bar") == None \ No newline at end of file + assert cache.hget("foo", "bar") == None From 432e1c9ea7622ebfd9edf02f1ac45f87e11aa744 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Tue, 13 Sep 2022 11:17:24 +0900 Subject: [PATCH 15/21] fix : delete print --- django_redis/client/default.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 9968a4a6..9a1e93d6 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -811,8 +811,7 @@ def hset( client, index = self.get_client( write=True, tried=tried, show_index=True ) - print(bool(client.hset(name, key, value, mapping))) - print(name,key,value,'set') + return bool(client.hset(name, key, value, mapping)) except _main_exceptions as e: if ( From c0215caee35822532f6b030c08ecd9632ebd7fdb Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Tue, 13 Sep 2022 11:31:54 +0900 Subject: [PATCH 16/21] fix : tabs and flake E711(comparison to None) --- django_redis/client/default.py | 255 ++++++++++++++++----------------- tests/test_backend.py | 2 +- 2 files changed, 128 insertions(+), 129 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index c66c73c7..030200cc 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,30 +774,29 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, - version: Optional[int] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, + version: Optional[int] = None, ) -> bool: name = self.make_key(name, version=version) if client is None: client = self.get_client(write=True) try: - return bool(client.hdel(name, *keys)) except _main_exceptions as e: raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Union[str, bytes], - value: Union[bytes, float, int, str], - version: Optional[int] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Union[str, bytes], + value: Union[bytes, float, int, str], + version: Optional[int] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -815,9 +814,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 @@ -825,12 +824,12 @@ def hset( raise ConnectionInterrupted(connection=client) from e def hget( - self, - name: str, - key: str, - default=None, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + name: str, + key: str, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> Any: if client is None: client = self.get_client(write=False) diff --git a/tests/test_backend.py b/tests/test_backend.py index a77b5a09..c347e446 100755 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -784,4 +784,4 @@ def test_hdel(self, cache: RedisCache): cache.hset("foo", "bar", "baz") cache.hset("foo", "baz", "bar") cache.hdel("foo", "bar") - assert cache.hget("foo", "bar") == None + assert cache.hget("foo", "bar") is None From 9d041f8f8a5564b1a36e26c7a83d4820b624be07 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 02:32:16 +0000 Subject: [PATCH 17/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/default.py | 254 ++++++++++++++++----------------- 1 file changed, 127 insertions(+), 127 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 030200cc..5f0d418f 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,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) @@ -275,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) @@ -299,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 @@ -317,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 @@ -335,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) @@ -357,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. @@ -375,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. @@ -401,7 +401,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. @@ -461,7 +461,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. @@ -489,11 +489,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 @@ -514,12 +514,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) @@ -566,12 +566,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 @@ -587,11 +587,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 @@ -600,7 +600,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. @@ -650,7 +650,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. @@ -666,11 +666,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 @@ -685,7 +685,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. @@ -704,7 +704,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 @@ -718,7 +718,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 @@ -749,11 +749,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. @@ -774,11 +774,11 @@ def touch( return bool(client.pexpire(key, timeout)) def hdel( - self, - name: Any, - *keys: Union[str, bytes], - client: Optional[Redis] = None, - version: Optional[int] = None, + self, + name: Any, + *keys: Union[str, bytes], + client: Optional[Redis] = None, + version: Optional[int] = None, ) -> bool: name = self.make_key(name, version=version) @@ -790,13 +790,13 @@ def hdel( raise ConnectionInterrupted(connection=client) from e def hset( - self, - name: Any, - key: Union[str, bytes], - value: Union[bytes, float, int, str], - version: Optional[int] = None, - mapping: Optional[dict] = None, - client: Optional[Redis] = None, + self, + name: Any, + key: Union[str, bytes], + value: Union[bytes, float, int, str], + version: Optional[int] = None, + mapping: Optional[dict] = None, + client: Optional[Redis] = None, ) -> bool: original_client = client @@ -814,9 +814,9 @@ def hset( return bool(client.hset(name, key, value, mapping)) 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 @@ -824,12 +824,12 @@ def hset( raise ConnectionInterrupted(connection=client) from e def hget( - self, - name: str, - key: str, - default=None, - version: Optional[int] = None, - client: Optional[Redis] = None, + self, + name: str, + key: str, + default=None, + version: Optional[int] = None, + client: Optional[Redis] = None, ) -> Any: if client is None: client = self.get_client(write=False) From 5077ebaffc1647952d7088a902e16d63489fa82a Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Tue, 13 Sep 2022 14:05:10 +0900 Subject: [PATCH 18/21] feat : add sharded interface --- django_redis/client/default.py | 6 ++---- django_redis/client/sharded.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_backend.py | 3 ++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 030200cc..d860da88 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -776,12 +776,11 @@ def touch( def hdel( self, name: Any, - *keys: Union[str, bytes], + keys: List, client: Optional[Redis] = None, version: Optional[int] = None, ) -> bool: name = self.make_key(name, version=version) - if client is None: client = self.get_client(write=True) try: @@ -805,7 +804,6 @@ def hset( value = self.encode(value) while True: try: - if client is None: client, index = self.get_client( write=True, tried=tried, show_index=True @@ -835,7 +833,7 @@ def hget( client = self.get_client(write=False) name = self.make_key(name, version=version) - print(name, key, "get") + try: value = client.hget(name, key) except _main_exceptions as e: diff --git a/django_redis/client/sharded.py b/django_redis/client/sharded.py index a1b0edd0..8b8d429a 100644 --- a/django_redis/client/sharded.py +++ b/django_redis/client/sharded.py @@ -323,3 +323,37 @@ def touch(self, key, timeout=DEFAULT_TIMEOUT, version=None, client=None): def clear(self, client=None): for connection in self._serverdict.values(): connection.flushdb() + + def hset( + self, name, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None + ): + """ + Persist a value to the cache, and set an optional expiration time. + """ + if client is None: + name = self.make_key(name, version=version) + client = self.get_server(name) + + return super().hset( + name=name, key=key, value=value, version=version, client=client + ) + + def hget( + self, name, key, version=None, client=None + ): + """ + Persist a value to the cache, and set an optional expiration time. + """ + if client is None: + name = self.make_key(name, version=version) + client = self.get_server(name) + + return super().hget( + name=name, key=key, version=version, client=client + ) + + def hdel(self, name, *keys, version=None, client=None): + if client is None: + name = self.make_key(name, version=version) + client = self.get_server(name) + return super().hdel(name=name, keys=keys, version=version, client=client) diff --git a/tests/test_backend.py b/tests/test_backend.py index c347e446..e2e9d738 100755 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -783,5 +783,6 @@ def test_hset_hget(self, cache: RedisCache): def test_hdel(self, cache: RedisCache): cache.hset("foo", "bar", "baz") cache.hset("foo", "baz", "bar") - cache.hdel("foo", "bar") + cache.hdel("foo", "bar", "baz") assert cache.hget("foo", "bar") is None + assert cache.hget("foo", "baz") is None From 85e5deb737358bbfe77c10e708ce0b3d1fda39f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 05:06:10 +0000 Subject: [PATCH 19/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/sharded.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/django_redis/client/sharded.py b/django_redis/client/sharded.py index 8b8d429a..d2616938 100644 --- a/django_redis/client/sharded.py +++ b/django_redis/client/sharded.py @@ -325,7 +325,7 @@ def clear(self, client=None): connection.flushdb() def hset( - self, name, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None + self, name, key, value, timeout=DEFAULT_TIMEOUT, version=None, client=None ): """ Persist a value to the cache, and set an optional expiration time. @@ -338,9 +338,7 @@ def hset( name=name, key=key, value=value, version=version, client=client ) - def hget( - self, name, key, version=None, client=None - ): + def hget(self, name, key, version=None, client=None): """ Persist a value to the cache, and set an optional expiration time. """ @@ -348,9 +346,7 @@ def hget( name = self.make_key(name, version=version) client = self.get_server(name) - return super().hget( - name=name, key=key, version=version, client=client - ) + return super().hget(name=name, key=key, version=version, client=client) def hdel(self, name, *keys, version=None, client=None): if client is None: From 10f5ad6f8c61df378f99790bf0a0baa689b5f3d4 Mon Sep 17 00:00:00 2001 From: Moonseok Lee <> Date: Tue, 13 Sep 2022 14:23:20 +0900 Subject: [PATCH 20/21] feat : hdel receive List --- django_redis/client/sharded.py | 4 ++-- tests/test_backend.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/django_redis/client/sharded.py b/django_redis/client/sharded.py index 8b8d429a..e80c330b 100644 --- a/django_redis/client/sharded.py +++ b/django_redis/client/sharded.py @@ -352,8 +352,8 @@ def hget( name=name, key=key, version=version, client=client ) - def hdel(self, name, *keys, version=None, client=None): + def hdel(self, name, keys, version=None, client=None): if client is None: name = self.make_key(name, version=version) client = self.get_server(name) - return super().hdel(name=name, keys=keys, version=version, client=client) + return super().hdel(name=name, keys=keys, version=version, client=client, ) diff --git a/tests/test_backend.py b/tests/test_backend.py index e2e9d738..c4e89c88 100755 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -783,6 +783,6 @@ def test_hset_hget(self, cache: RedisCache): def test_hdel(self, cache: RedisCache): cache.hset("foo", "bar", "baz") cache.hset("foo", "baz", "bar") - cache.hdel("foo", "bar", "baz") + cache.hdel("foo", ["bar", "baz"]) assert cache.hget("foo", "bar") is None assert cache.hget("foo", "baz") is None From b6115c245b3b162d54fef83909d27646bde2654d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 05:23:48 +0000 Subject: [PATCH 21/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- django_redis/client/sharded.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/django_redis/client/sharded.py b/django_redis/client/sharded.py index d53943d4..7262bfb5 100644 --- a/django_redis/client/sharded.py +++ b/django_redis/client/sharded.py @@ -352,4 +352,9 @@ def hdel(self, name, keys, version=None, client=None): if client is None: name = self.make_key(name, version=version) client = self.get_server(name) - return super().hdel(name=name, keys=keys, version=version, client=client, ) + return super().hdel( + name=name, + keys=keys, + version=version, + client=client, + )