Commit 74b834c6 authored by Tomi Suomela's avatar Tomi Suomela

Touch & touch pattern: if stale_cache_timeout is set, use it (+timeout) on…

Touch & touch pattern: if stale_cache_timeout is set, use it (+timeout) on actual key even if the __fresh__ key is not found
parent aa2ffa69
......@@ -367,11 +367,10 @@ class BaseRedisCache(BaseCache):
def touch(self, client, key, timeout=DEFAULT_TIMEOUT, stale_cache_timeout=None):
"""Reset the timeout of a key to `timeout` seconds."""
fresh_key = "__fresh__" + key
key_timeout = timeout
key_timeout = (
timeout if stale_cache_timeout is None else timeout + stale_cache_timeout
)
if client.exists(fresh_key):
key_timeout = (
None if stale_cache_timeout is None else timeout + stale_cache_timeout
)
client.expire(fresh_key, timeout)
return client.expire(key, key_timeout)
......@@ -529,17 +528,21 @@ class BaseRedisCache(BaseCache):
def _touch_pattern(self, client, pattern, timeout=DEFAULT_TIMEOUT, stale_cache_timeout=None):
"""Reset the timeout of a keys to `timeout` seconds."""
keys = list(client.scan_iter(match=pattern))
key_timeout = (
timeout if stale_cache_timeout is None else timeout + stale_cache_timeout
)
for key in keys:
decoded_key = key.decode()
key_timeout = timeout
fresh_key = "__fresh__" + decoded_key
if client.exists(fresh_key):
key_timeout = (
None if stale_cache_timeout is None else timeout + stale_cache_timeout
)
client.expire(fresh_key, timeout)
client.expire(decoded_key, key_timeout)
def touch_pattern(self, client, pattern, timeout=DEFAULT_TIMEOUT, stale_cache_timeout=None):
raise NotImplementedError
@get_client()
def has_fresh_key(self, client, key):
"""Returns True if the fresh key is in the cache and has not expired."""
return client.exists("__fresh__" + key)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment