enable LocalCacheProxy only if reuqetss will take place

This commit is contained in:
Laura Klünder 2025-04-17 22:25:23 +02:00
parent 8696723da1
commit a543380fd3
2 changed files with 14 additions and 4 deletions

View file

@ -1,7 +1,7 @@
import re import re
from functools import wraps from functools import wraps
from c3nav.mapdata.utils.cache.local import per_request_cache from c3nav.mapdata.utils.cache.local import per_request_cache, LocalCacheProxy
from c3nav.mapdata.utils.user import get_user_data_lazy from c3nav.mapdata.utils.user import get_user_data_lazy
@ -64,6 +64,7 @@ class RequestCacheMiddleware:
""" """
def __init__(self, get_response): def __init__(self, get_response):
self.get_response = get_response self.get_response = get_response
LocalCacheProxy.enable_globally()
def __call__(self, request): def __call__(self, request):
per_request_cache.clear() per_request_cache.clear()

View file

@ -41,17 +41,26 @@ class LocalCacheProxy:
self._items.pop(next(iter(self._items.keys()))) self._items.pop(next(iter(self._items.keys())))
def _check_mapupdate(self): def _check_mapupdate(self):
# todo: would be nice to not need this… why do we need this? # todo: thanks to enable_globally() we shouldn't need this any more
from c3nav.mapdata.models import MapUpdate from c3nav.mapdata.models import MapUpdate
mapupdate = MapUpdate.current_cache_key() mapupdate = MapUpdate.current_cache_key()
if self._mapupdate != mapupdate: if self._mapupdate != mapupdate:
self._items = OrderedDict() self._items = OrderedDict()
self._mapupdate = mapupdate 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.
"""
LocalCacheProxy.enabled = False
def set(self, key, value, expire): def set(self, key, value, expire):
self._check_mapupdate() self._check_mapupdate()
cache.set(key, value, expire) cache.set(key, value, expire)
if LocalCacheProxy.enabled:
self._items[key] = value self._items[key] = value
self._prune() self._prune()