Ticket #16006: lazycache.diff

File lazycache.diff, 1.5 KB (added by brianjaystanley, 4 years ago)
  • django/core/cache/__init__.py

     
    2020    InvalidCacheBackendError, CacheKeyWarning, BaseCache)
    2121from django.core.exceptions import ImproperlyConfigured
    2222from django.utils import importlib
     23from django.utils.functional import SimpleLazyObject
    2324
    2425try:
    2526    # The mod_python version is more efficient, so try importing it first.
     
    177178    except (AttributeError, ImportError), e:
    178179        raise InvalidCacheBackendError(
    179180            "Could not find backend '%s': %s" % (backend, e))
    180     return backend_cls(location, params)
     181    cache = backend_cls(location, params)
     182    # Some caches -- python-memcached in particular -- need to do a cleanup at the
     183    # end of a request cycle. If the cache provides a close() method, wire it up
     184    # here.
     185    if hasattr(cache, 'close'):
     186        signals.request_finished.connect(cache.close, dispatch_uid='%s.%s' % (backend_cls.__module__, backend_cls.__name__))
     187    return cache   
    181188
    182 cache = get_cache(DEFAULT_CACHE_ALIAS)
    183 
    184 # Some caches -- python-memcached in particular -- need to do a cleanup at the
    185 # end of a request cycle. If the cache provides a close() method, wire it up
    186 # here.
    187 if hasattr(cache, 'close'):
    188     signals.request_finished.connect(cache.close)
     189cache = SimpleLazyObject(lambda: get_cache(DEFAULT_CACHE_ALIAS))
Back to Top