Settings
cache-results is configured by one optional Django setting, CACHE_RESULTS,
a dict. Every entry is optional, and an empty or missing setting means the
defaults below.
# settings.py
CACHE_RESULTS = {
"CACHE_ALIAS": "default",
"TIMEOUT": 3600,
"KEY_PREFIX": "",
"VERSION": None,
"KEY_ENCODER": "cache_results.keys.CacheKeyEncoder",
"LOGGING_ENABLED": None,
"VERBOSE_LOGGING": None,
"LOG_PREFIX": None,
}
The setting is read on every call, so
override_settings() works in tests, and changes made by a
settings reload take effect at once.
Entries
CACHE_ALIASThe name, in your
CACHESsetting, of the cache that holds cached results. Default:"default". A cache of their own lets you clear cached results without clearing sessions or anything else.TIMEOUTThe timeout, in seconds, of a decorated function that names none. Default: the cache’s own
TIMEOUT(300 seconds unless you set it inCACHES). A decorator’s owntimeoutalways wins.KEY_PREFIXText that starts every key. Default: none.
VERSIONWhen set,
v<VERSION>:follows the prefix in every key. Change it, orKEY_PREFIX, to make every cached result a miss at once. Default: none.KEY_ENCODERA subclass of
CacheKeyEncoder, which allows more argument types. See Arguments and Cache Keys. Default:CacheKeyEncoder. Anything that isn’t a subclass of it raisesTypeError, so that a misconfiguration can’t make unsafe keys.LOGGING_ENABLEDA callable without arguments that returns whether to log every cache hit, miss and invalidation at
INFO, or a plainTrue/False. Default: off. See Logging.VERBOSE_LOGGINGThe same, for whether those messages include the arguments. Default: off.
LOG_PREFIXA callable
(text, event)that formats the start of each log message, whereeventis"hit","miss","invalidate"or"". Default: the text unchanged.
Callables and classes are given as dotted paths ("myapp.cache.logging_on"),
imported once, or as the objects themselves.
Decorator options
@cache_results(timeout=None, cache_key=None, logging_enabled=False)
timeoutSeconds.
Noneuses theTIMEOUTsetting.0caches nothing.cache_keyA fixed key that every call shares, whatever its arguments. See Arguments and Cache Keys.
logging_enabledLog this function’s hits, misses and invalidations at
INFO, even whenLOGGING_ENABLEDis off.