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
|
|
|
"""
|
2010-12-21 23:19:19 +08:00
|
|
|
from django.core import signals
|
|
|
|
from django.core.cache.backends.base import (
|
2020-05-20 17:45:31 +08:00
|
|
|
BaseCache, CacheKeyWarning, InvalidCacheBackendError, InvalidCacheKey,
|
2015-06-16 02:07:31 +08:00
|
|
|
)
|
2017-10-22 23:30:42 +08:00
|
|
|
from django.utils.connection import BaseConnectionHandler, ConnectionProxy
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2013-02-03 05:58:02 +08:00
|
|
|
|
2010-12-21 23:19:19 +08:00
|
|
|
__all__ = [
|
2018-05-28 21:14:46 +08:00
|
|
|
'cache', 'caches', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError',
|
2020-05-20 17:45:31 +08:00
|
|
|
'CacheKeyWarning', 'BaseCache', 'InvalidCacheKey',
|
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'
|
|
|
|
|
2006-02-24 14:07:01 +08:00
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
class CacheHandler(BaseConnectionHandler):
|
|
|
|
settings_name = 'CACHES'
|
|
|
|
exception_class = InvalidCacheBackendError
|
2013-10-19 06:49:24 +08:00
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
def create_connection(self, alias):
|
|
|
|
params = self.settings[alias].copy()
|
2020-12-07 19:34:09 +08:00
|
|
|
backend = params.pop('BACKEND')
|
|
|
|
location = params.pop('LOCATION', '')
|
|
|
|
try:
|
|
|
|
backend_cls = import_string(backend)
|
|
|
|
except ImportError as e:
|
|
|
|
raise InvalidCacheBackendError(
|
|
|
|
"Could not find backend '%s': %s" % (backend, e)
|
2017-10-22 23:30:42 +08:00
|
|
|
) from e
|
|
|
|
return backend_cls(location, params)
|
2013-11-24 23:19:47 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
caches = CacheHandler()
|
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS)
|
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()
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2013-11-24 23:19:47 +08:00
|
|
|
signals.request_finished.connect(close_caches)
|