team-3/src/c3nav/mapdata/utils/cache/local.py

85 lines
2.8 KiB
Python
Raw Normal View History

from collections import OrderedDict
from contextvars import ContextVar
from django.core.cache import cache
2024-12-27 18:50:36 +01:00
from django.conf import settings
2018-12-27 22:02:07 +01:00
class NoneFromCache:
pass
class LocalCacheProxy:
# django cache, buffered using a LRU cache
# only usable for stuff that never needs to know about changes made by other cache clients, obviously
def __init__(self, maxsize=128):
self._maxsize = maxsize
2018-12-27 22:02:07 +01:00
self._mapupdate = None
self._items: ContextVar[OrderedDict] = ContextVar("cache items")
# do not use a default of a dict, this can lead to same instance in different contexts
# we don't particularly care about this for LocalCacheProxy,
# but we DEFINITELY care about this for the local request cache.
# Most importantly, this is why the clear function always sets a new dictionary to be extra sure.
self.clear()
def get(self, key, default=None):
if self._mapupdate is None:
self._check_mapupdate()
try:
# first check out cache
result = self._items.get()[key]
except (KeyError, LookupError):
# not in our cache
result = cache.get(key, default=NoneFromCache)
if result is not NoneFromCache:
self._items.get()[key] = result
self._prune()
else:
result = default
else:
self._items.get().move_to_end(key, last=True)
return result
def _prune(self):
# remove old items
while len(self._items.get()) > self._maxsize:
self._items.get().pop(next(iter(self._items.get().keys())))
2018-12-27 22:02:07 +01:00
def _check_mapupdate(self):
# todo: thanks to enable_globally() we shouldn't need this any more
2024-12-27 18:50:36 +01:00
from c3nav.mapdata.models import MapUpdate
2018-12-27 22:02:07 +01:00
mapupdate = MapUpdate.current_cache_key()
if self._mapupdate != mapupdate:
self.clear()
2018-12-27 22:02:07 +01:00
self._mapupdate = mapupdate
enabled = False
@classmethod
def enable_globally(cls):
"""
This gets called when the per request cache middleware is loaded.
We don't want local cache proxies to work outside of requests.
"""
2025-04-17 23:18:19 +02:00
LocalCacheProxy.enabled = True
def set(self, key, value, expire):
2018-12-27 22:02:07 +01:00
self._check_mapupdate()
cache.set(key, value, expire)
if LocalCacheProxy.enabled:
self._items.get()[key] = value
self._prune()
2024-12-27 18:50:36 +01:00
def clear(self):
self._items.set(OrderedDict())
2024-12-27 18:50:36 +01:00
class RequestLocalCacheProxy(LocalCacheProxy):
""" this is a subclass without prune, to be cleared after every request """
def _prune(self):
pass
def _check_mapupdate(self):
pass
per_request_cache = RequestLocalCacheProxy(maxsize=settings.CACHE_SIZE_LOCATIONS)