diff options
author | Jordan Cook <jordan.cook.git@proton.me> | 2022-09-29 15:28:04 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook.git@proton.me> | 2022-09-30 16:00:16 -0500 |
commit | 747f472e7a50274f45e414c9ee4f8fe70f6f1131 (patch) | |
tree | 9161e6288f3dcdb09ef636e99db470b9f9fcd203 /requests_cache | |
parent | e06d915d12f97a72b932be7dc67ccf4b80077324 (diff) | |
download | requests-cache-747f472e7a50274f45e414c9ee4f8fe70f6f1131.tar.gz |
Add ttl_offset argument for Redis backend
Diffstat (limited to 'requests_cache')
-rw-r--r-- | requests_cache/backends/redis.py | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/requests_cache/backends/redis.py b/requests_cache/backends/redis.py index 95b44b5..259b858 100644 --- a/requests_cache/backends/redis.py +++ b/requests_cache/backends/redis.py @@ -14,26 +14,33 @@ from ..cache_keys import decode, encode from ..serializers import utf8_encoder from . import BaseCache, BaseStorage +DEFAULT_TTL_OFFSET = 3600 logger = getLogger(__name__) -# TODO: TTL tests -# TODO: Option to set a TTL offset, for longer expiration than expire_after class RedisCache(BaseCache): """Redis cache backend. Args: namespace: Redis namespace connection: Redis connection instance to use instead of creating a new one - ttl: Use Redis TTL to automatically remove expired items + ttl: Use Redis TTL to automatically delete expired items + ttl_offset: Additional time to wait before deleting expired items, in seconds kwargs: Additional keyword arguments for :py:class:`redis.client.Redis` """ def __init__( - self, namespace='http_cache', connection: Redis = None, ttl: bool = True, **kwargs + self, + namespace='http_cache', + connection: Redis = None, + ttl: bool = True, + ttl_offset: int = DEFAULT_TTL_OFFSET, + **kwargs, ): super().__init__(cache_name=namespace, **kwargs) - self.responses = RedisDict(namespace, connection=connection, ttl=ttl, **kwargs) + self.responses = RedisDict( + namespace, connection=connection, ttl=ttl, ttl_offset=ttl_offset, **kwargs + ) kwargs.pop('serializer', None) self.redirects = RedisHashDict( namespace, @@ -58,6 +65,7 @@ class RedisDict(BaseStorage): collection_name: str = None, connection=None, ttl: bool = True, + ttl_offset: int = DEFAULT_TTL_OFFSET, **kwargs, ): @@ -66,6 +74,7 @@ class RedisDict(BaseStorage): self.connection = connection or StrictRedis(**connection_kwargs) self.namespace = namespace self.ttl = ttl + self.ttl_offset = ttl_offset def _bkey(self, key: str) -> bytes: """Get a full hash key as bytes""" @@ -86,8 +95,9 @@ class RedisDict(BaseStorage): def __setitem__(self, key, item): """Save an item to the cache, optionally with TTL""" expires_delta = getattr(item, 'expires_delta', None) - if self.ttl and (expires_delta or 0) > 0: - self.connection.setex(self._bkey(key), expires_delta, self.serialize(item)) + ttl_seconds = (expires_delta or 0) + self.ttl_offset + if self.ttl and ttl_seconds > 0: + self.connection.setex(self._bkey(key), ttl_seconds, self.serialize(item)) else: self.connection.set(self._bkey(key), self.serialize(item)) |