2006-02-24 14:07:01 +08:00
|
|
|
"""
|
|
|
|
Caching framework.
|
|
|
|
|
|
|
|
This package defines set of cache backends that all conform to a simple API.
|
|
|
|
In a nutshell, a cache is a set of values -- which can be any object that
|
|
|
|
may be pickled -- identified by string keys. For the complete API, see
|
|
|
|
the abstract BaseCache class in django.core.cache.backends.base.
|
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
Client code should use the `cache` variable defined here to access the default
|
|
|
|
cache backend and look up non-default cache backends in the `caches` dict-like
|
|
|
|
object.
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
See docs/topics/cache.txt for information on the public API.
|
2006-02-24 14:07:01 +08:00
|
|
|
"""
|
2013-10-19 06:49:24 +08:00
|
|
|
from threading import local
|
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import signals
|
|
|
|
from django.core.cache.backends.base import (
|
|
|
|
InvalidCacheBackendError, CacheKeyWarning, BaseCache)
|
2011-01-15 14:12:56 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2013-02-03 05:58:02 +08:00
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
__all__ = [
|
2014-12-27 00:46:48 +08:00
|
|
|
'cache', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError',
|
2013-10-18 19:25:30 +08:00
|
|
|
'CacheKeyWarning', 'BaseCache',
|
2010-12-21 23:19:19 +08:00
|
|
|
]
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
DEFAULT_CACHE_ALIAS = 'default'
|
|
|
|
|
|
|
|
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
|
|
|
|
raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS)
|
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
def _create_cache(backend, **kwargs):
|
2010-12-21 23:19:19 +08:00
|
|
|
try:
|
2013-09-19 16:38:56 +08:00
|
|
|
# Try to get the CACHES entry for the given backend name first
|
|
|
|
try:
|
|
|
|
conf = settings.CACHES[backend]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
# Trying to import the given backend, in case it's a dotted path
|
2014-01-21 04:15:14 +08:00
|
|
|
import_string(backend)
|
|
|
|
except ImportError as e:
|
2013-09-19 16:38:56 +08:00
|
|
|
raise InvalidCacheBackendError("Could not find backend '%s': %s" % (
|
|
|
|
backend, e))
|
|
|
|
location = kwargs.pop('LOCATION', '')
|
|
|
|
params = kwargs
|
2010-12-21 23:19:19 +08:00
|
|
|
else:
|
2013-09-19 16:38:56 +08:00
|
|
|
params = conf.copy()
|
|
|
|
params.update(kwargs)
|
|
|
|
backend = params.pop('BACKEND')
|
|
|
|
location = params.pop('LOCATION', '')
|
2014-01-21 04:15:14 +08:00
|
|
|
backend_cls = import_string(backend)
|
|
|
|
except ImportError as e:
|
2010-12-21 23:19:19 +08:00
|
|
|
raise InvalidCacheBackendError(
|
|
|
|
"Could not find backend '%s': %s" % (backend, e))
|
2013-11-24 23:19:47 +08:00
|
|
|
return backend_cls(location, params)
|
2010-12-21 23:19:19 +08:00
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
|
|
|
|
class CacheHandler(object):
|
|
|
|
"""
|
|
|
|
A Cache Handler to manage access to Cache instances.
|
|
|
|
|
|
|
|
Ensures only one instance of each alias exists per thread.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self._caches = local()
|
|
|
|
|
|
|
|
def __getitem__(self, alias):
|
|
|
|
try:
|
2013-11-24 23:19:47 +08:00
|
|
|
return self._caches.caches[alias]
|
2013-10-19 06:49:24 +08:00
|
|
|
except AttributeError:
|
2013-11-24 23:19:47 +08:00
|
|
|
self._caches.caches = {}
|
|
|
|
except KeyError:
|
2013-10-19 06:49:24 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
if alias not in settings.CACHES:
|
|
|
|
raise InvalidCacheBackendError(
|
|
|
|
"Could not find config for '%s' in settings.CACHES" % alias
|
|
|
|
)
|
|
|
|
|
|
|
|
cache = _create_cache(alias)
|
2013-11-24 23:19:47 +08:00
|
|
|
self._caches.caches[alias] = cache
|
2013-10-19 06:49:24 +08:00
|
|
|
return cache
|
|
|
|
|
2013-11-24 23:19:47 +08:00
|
|
|
def all(self):
|
|
|
|
return getattr(self._caches, 'caches', {}).values()
|
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
caches = CacheHandler()
|
|
|
|
|
2013-11-24 00:26:11 +08:00
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
class DefaultCacheProxy(object):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return caches[DEFAULT_CACHE_ALIAS] != other
|
|
|
|
|
|
|
|
cache = DefaultCacheProxy()
|
2013-11-24 23:19:47 +08:00
|
|
|
|
2013-11-25 11:07:21 +08:00
|
|
|
|
2013-11-24 23:19:47 +08:00
|
|
|
def close_caches(**kwargs):
|
|
|
|
# Some caches -- python-memcached in particular -- need to do a cleanup at the
|
|
|
|
# end of a request cycle. If not implemented in a particular backend
|
|
|
|
# cache.close is a no-op
|
|
|
|
for cache in caches.all():
|
|
|
|
cache.close()
|
|
|
|
signals.request_finished.connect(close_caches)
|