diff --git a/src/c3nav/mapdata/middleware.py b/src/c3nav/mapdata/middleware.py index a827dfaa..aae34fdf 100644 --- a/src/c3nav/mapdata/middleware.py +++ b/src/c3nav/mapdata/middleware.py @@ -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() diff --git a/src/c3nav/mapdata/utils/cache/local.py b/src/c3nav/mapdata/utils/cache/local.py index 9c1a6271..0e660f24 100644 --- a/src/c3nav/mapdata/utils/cache/local.py +++ b/src/c3nav/mapdata/utils/cache/local.py @@ -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):