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
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
@ -64,6 +64,7 @@ class RequestCacheMiddleware:
"""
def __init__(self, get_response):
self.get_response = get_response
LocalCacheProxy.enable_globally()
def __call__(self, request):
per_request_cache.clear()

View file

@ -41,18 +41,27 @@ class LocalCacheProxy:
self._items.pop(next(iter(self._items.keys())))
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
mapupdate = MapUpdate.current_cache_key()
if self._mapupdate != mapupdate:
self._items = OrderedDict()
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):
self._check_mapupdate()
cache.set(key, value, expire)
self._items[key] = value
if LocalCacheProxy.enabled:
self._items[key] = value
self._prune()
def clear(self):