diff options
| author | Florian Apolloner <florian@apolloner.eu> | 2017-10-22 17:30:42 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-12-08 08:55:44 +0100 |
| commit | 98e05ccde440cc9b768952cc10bc8285f4924e1f (patch) | |
| tree | e15f6086fb47418e5de6d4b61e079ff98fbc09ab /django/core/cache | |
| parent | 0f00560d45ab2931647b0cbe44a27c37c576c6dc (diff) | |
Fixed #32233 -- Cleaned-up duplicate connection functionality.
Diffstat (limited to 'django/core/cache')
| -rw-r--r-- | django/core/cache/__init__.py | 65 |
1 files changed, 9 insertions, 56 deletions
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py index b4bb2086c5..1da58153f4 100644 --- a/django/core/cache/__init__.py +++ b/django/core/cache/__init__.py @@ -12,13 +12,11 @@ object. See docs/topics/cache.txt for information on the public API. """ -from asgiref.local import Local - -from django.conf import settings from django.core import signals from django.core.cache.backends.base import ( BaseCache, CacheKeyWarning, InvalidCacheBackendError, InvalidCacheKey, ) +from django.utils.connection import BaseConnectionHandler, ConnectionProxy from django.utils.module_loading import import_string __all__ = [ @@ -29,28 +27,12 @@ __all__ = [ DEFAULT_CACHE_ALIAS = 'default' -class CacheHandler: - """ - A Cache Handler to manage access to Cache instances. - - Ensure only one instance of each alias exists per thread. - """ - def __init__(self): - self._caches = Local() - - def __getitem__(self, alias): - try: - return self._caches.caches[alias] - except AttributeError: - self._caches.caches = {} - except KeyError: - pass +class CacheHandler(BaseConnectionHandler): + settings_name = 'CACHES' + exception_class = InvalidCacheBackendError - if alias not in settings.CACHES: - raise InvalidCacheBackendError( - "Could not find config for '%s' in settings.CACHES" % alias - ) - params = settings.CACHES[alias].copy() + def create_connection(self, alias): + params = self.settings[alias].copy() backend = params.pop('BACKEND') location = params.pop('LOCATION', '') try: @@ -58,42 +40,13 @@ class CacheHandler: except ImportError as e: raise InvalidCacheBackendError( "Could not find backend '%s': %s" % (backend, e) - ) - cache = backend_cls(location, params) - self._caches.caches[alias] = cache - return cache - - def all(self): - return getattr(self._caches, 'caches', {}).values() + ) from e + return backend_cls(location, params) caches = CacheHandler() - -class DefaultCacheProxy: - """ - Proxy access to the default Cache object's attributes. - - This allows the legacy `cache` object to be thread-safe using the new - ``caches`` API. - """ - def __getattr__(self, name): - return getattr(caches[DEFAULT_CACHE_ALIAS], name) - - def __setattr__(self, name, value): - return setattr(caches[DEFAULT_CACHE_ALIAS], name, value) - - def __delattr__(self, name): - return delattr(caches[DEFAULT_CACHE_ALIAS], name) - - def __contains__(self, key): - return key in caches[DEFAULT_CACHE_ALIAS] - - def __eq__(self, other): - return caches[DEFAULT_CACHE_ALIAS] == other - - -cache = DefaultCacheProxy() +cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS) def close_caches(**kwargs): |
