2018-12-27 17:24:53 +01:00
|
|
|
from collections import OrderedDict
|
2025-04-17 23:25:51 +02:00
|
|
|
from contextvars import ContextVar
|
2018-12-27 17:24:53 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-27 17:24:53 +01:00
|
|
|
|
|
|
|
class NoneFromCache:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LocalCacheProxy:
|
|
|
|
# django cache, buffered using a LRU cache
|
2025-04-17 23:25:51 +02:00
|
|
|
# only usable for stuff that never needs to know about changes made by other cache clients, obviously
|
2018-12-27 17:24:53 +01:00
|
|
|
def __init__(self, maxsize=128):
|
|
|
|
self._maxsize = maxsize
|
2018-12-27 22:02:07 +01:00
|
|
|
self._mapupdate = None
|
2025-04-17 23:25:51 +02:00
|
|
|
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()
|
2018-12-27 17:24:53 +01:00
|
|
|
|
|
|
|
def get(self, key, default=None):
|
2018-12-28 17:29:49 +01:00
|
|
|
if self._mapupdate is None:
|
|
|
|
self._check_mapupdate()
|
2018-12-27 17:24:53 +01:00
|
|
|
try:
|
|
|
|
# first check out cache
|
2025-04-17 23:25:51 +02:00
|
|
|
result = self._items.get()[key]
|
2025-08-01 20:43:04 +02:00
|
|
|
except (KeyError, LookupError):
|
2018-12-27 17:24:53 +01:00
|
|
|
# not in our cache
|
|
|
|
result = cache.get(key, default=NoneFromCache)
|
|
|
|
if result is not NoneFromCache:
|
2025-04-17 23:25:51 +02:00
|
|
|
self._items.get()[key] = result
|
2018-12-27 17:24:53 +01:00
|
|
|
self._prune()
|
|
|
|
else:
|
|
|
|
result = default
|
|
|
|
else:
|
2025-04-17 23:25:51 +02:00
|
|
|
self._items.get().move_to_end(key, last=True)
|
2018-12-27 17:24:53 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _prune(self):
|
|
|
|
# remove old items
|
2025-04-17 23:25:51 +02:00
|
|
|
while len(self._items.get()) > self._maxsize:
|
|
|
|
self._items.get().pop(next(iter(self._items.get().keys())))
|
2018-12-27 17:24:53 +01:00
|
|
|
|
2018-12-27 22:02:07 +01:00
|
|
|
def _check_mapupdate(self):
|
2025-04-17 22:25:23 +02:00
|
|
|
# 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:
|
2025-04-17 23:25:51 +02:00
|
|
|
self.clear()
|
2018-12-27 22:02:07 +01:00
|
|
|
self._mapupdate = mapupdate
|
|
|
|
|
2025-04-17 22:25:23 +02:00
|
|
|
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
|
2025-04-17 22:25:23 +02:00
|
|
|
|
2018-12-27 17:24:53 +01:00
|
|
|
def set(self, key, value, expire):
|
2018-12-27 22:02:07 +01:00
|
|
|
self._check_mapupdate()
|
2018-12-27 17:24:53 +01:00
|
|
|
cache.set(key, value, expire)
|
2025-04-17 22:25:23 +02:00
|
|
|
if LocalCacheProxy.enabled:
|
2025-04-17 23:25:51 +02:00
|
|
|
self._items.get()[key] = value
|
2018-12-27 17:24:53 +01:00
|
|
|
self._prune()
|
2024-12-27 18:50:36 +01:00
|
|
|
|
|
|
|
def clear(self):
|
2025-04-17 23:25:51 +02:00
|
|
|
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)
|