This creates a simple LRU policy to evict items from the cache that have
not been accessed in 24 hours.
Fixes: #13547
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
return photo
# Compose the cache key
return photo
# Compose the cache key
- key = "accounts:%s:avatar:%s:%s:%s" \
+ cache_key = "accounts:%s:avatar:%s:%s:%s" \
% (self.uid, self.avatar_hash, format or "N/A", size)
% (self.uid, self.avatar_hash, format or "N/A", size)
- # Try to retrieve something from the cache
- avatar = await self.backend.cache.get(key)
+ # Try to fetch the data from the cache
+ async with await self.backend.cache.pipeline() as p:
+ # Fetch the key
+ await p.get(cache_key)
+
+ # Reset the TTL
+ await p.expire(cache_key, 86400)
+
+ # Execute the pipeline
+ avatar, _ = await p.execute()
+
+ # Return the cached value (if any)
avatar = util.generate_thumbnail(photo, size, square=True, format=format)
# Save to cache for 24h
avatar = util.generate_thumbnail(photo, size, square=True, format=format)
# Save to cache for 24h
- await self.backend.cache.set(key, avatar, 86400)
+ await self.backend.cache.set(cache_key, avatar, 86400)
async def get_thumbnail(self, size, format=None):
assert self.is_bitmap_image()
async def get_thumbnail(self, size, format=None):
assert self.is_bitmap_image()
+ # Let thumbnails live in the cache for up to 24h
+ ttl = 24 * 3600
+
cache_key = ":".join((
"wiki",
"thumbnail",
cache_key = ":".join((
"wiki",
"thumbnail",
))
# Try to fetch the data from the cache
))
# Try to fetch the data from the cache
- thumbnail = await self.backend.cache.get(cache_key)
+ async with await self.backend.cache.pipeline() as p:
+ # Fetch the key
+ await p.get(cache_key)
+
+ # Reset the TTL
+ await p.expire(cache_key, ttl)
+
+ # Execute the pipeline
+ thumbnail, _ = await p.execute()
+
+ # Return the cached value
if thumbnail:
return thumbnail
# Generate the thumbnail
thumbnail = util.generate_thumbnail(self.blob, size, format=format, quality=95)
if thumbnail:
return thumbnail
# Generate the thumbnail
thumbnail = util.generate_thumbnail(self.blob, size, format=format, quality=95)
- # Put it into the cache for forever
- await self.backend.cache.set(cache_key, thumbnail)
+ # Put it into the cache for 24h
+ await self.backend.cache.set(cache_key, thumbnail, ttl)
- key = "avatar:letter:%s:%s:%s" % ("".join(letters), format or "N/A", size)
+ cache_key = "avatar:letter:%s:%s:%s" % ("".join(letters), format or "N/A", size)
+
+ # Try to fetch the data from the cache
+ async with await self.backend.cache.pipeline() as p:
+ # Fetch the key
+ await p.get(cache_key)
+
+ # Reset the TTL
+ await p.expire(cache_key, 86400)
+
+ # Execute the pipeline
+ avatar, _ = await p.execute()
- # Fetch avatar from the cache
- avatar = await self.backend.cache.get(key)
if not avatar:
avatar = self._make_avatar(letters, size=size, **args)
# Cache for forever
if not avatar:
avatar = self._make_avatar(letters, size=size, **args)
# Cache for forever
- await self.backend.cache.set(key, avatar)
+ await self.backend.cache.set(cache_key, avatar, 86400)