diff options
author | 2022-04-30 18:28:35 +0200 | |
---|---|---|
committer | 2022-04-30 18:50:17 +0200 | |
commit | ab7d4b608604e4d9fdba07001f6d93ef1efe85e3 (patch) | |
tree | 1e9ae3a6fedbce6734ce38b065caf3ff9d0069dc | |
parent | Expire key cache values when redis key expires (diff) |
expire internal cache according to redis' TTL instead of always a week
-rw-r--r-- | bot/exts/info/doc/_redis_cache.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/bot/exts/info/doc/_redis_cache.py b/bot/exts/info/doc/_redis_cache.py index aba23ff68..95d6630f0 100644 --- a/bot/exts/info/doc/_redis_cache.py +++ b/bot/exts/info/doc/_redis_cache.py @@ -7,11 +7,15 @@ from typing import Optional, TYPE_CHECKING from async_rediscache.types.base import RedisObject, namespace_lock +from bot.log import get_logger + if TYPE_CHECKING: from ._cog import DocItem WEEK_SECONDS = datetime.timedelta(weeks=1).total_seconds() +log = get_logger(__name__) + class DocRedisCache(RedisObject): """Interface for redis functionality needed by the Doc cog.""" @@ -34,19 +38,22 @@ class DocRedisCache(RedisObject): set_expire = self._set_expires.get(redis_key) if set_expire is None: # An expire is only set if the key didn't exist before. - # If this is the first time setting values for this key check if it exists and add it to - # `_set_expires` to prevent redundant checks for subsequent uses with items from the same page. - needs_expire = not await connection.exists(redis_key) + ttl = await connection.ttl(redis_key) - elif time.monotonic() - set_expire > WEEK_SECONDS: + if ttl == -1: + log.warning(f"Key `{redis_key}` had no expire set.") + if ttl < 0: # not set or didn't exist + needs_expire = True + else: + self._set_expires[redis_key] = time.monotonic() + ttl - .1 # we need this to expire before redis + + elif time.monotonic() > set_expire: # If we got here the key expired in redis and we can be sure it doesn't exist. needs_expire = True - del self._set_expires[redis_key] - - self._set_expires[redis_key] = time.monotonic() await connection.hset(redis_key, item.symbol_id, value) if needs_expire: + self._set_expires[redis_key] = time.monotonic() + WEEK_SECONDS await connection.expire(redis_key, WEEK_SECONDS) @namespace_lock |