Settings

Configuration of the @cache_results decorator, read from the.

CACHE_RESULTS Django setting. Every entry is optional:

CACHE_RESULTS = {
    # the CACHES alias that holds cached results.
    "CACHE_ALIAS": "default",
    # the default timeout, in seconds, of a decorated function that names none.
    # Unset, it is the cache alias's own TIMEOUT.
    "TIMEOUT": 3600,
    # text that starts every key. Changing it, or VERSION, abandons every cached
    # result at once, without touching anything else in the same cache.
    "KEY_PREFIX": "",
    "VERSION": None,
    # a subclass of cache_results.keys.CacheKeyEncoder, to allow more argument types.
    "KEY_ENCODER": "cache_results.keys.CacheKeyEncoder",
    # zero-argument callables that return whether to log cache hits, misses and
    # invalidations, and whether those logs include the arguments.
    "LOGGING_ENABLED": None,
    "VERBOSE_LOGGING": None,
    # a callable(text, event) that formats the start of each log message, where
    # event is "hit", "miss", "invalidate" or "".
    "LOG_PREFIX": None,
}

Callables and classes are given as dotted paths, or as the objects themselves. Settings are read on every call, so override_settings works in tests. This module depends only on Django and the standard library.

class cache_results.conf.CacheResultsSettings[source]

Bases: object

The CACHE_RESULTS setting, with its defaults.

Use the module’s cache_results_settings instance.

property cache_alias: str

The CACHES alias that holds cached results.

Return type:

str

property timeout: float | None

The default timeout in seconds, or Django’s DEFAULT_TIMEOUT marker, which means the cache alias’s own.

Return type:

Optional[float]

property key_prefix: str

Text that starts every key: KEY_PREFIX, then v<VERSION>: when a version is set.

Return type:

str

property key_encoder: type[CacheKeyEncoder]

The JSON encoder for arguments.

Return type:

type[CacheKeyEncoder]

Raises:

TypeError – If the setting doesn’t name a subclass of CacheKeyEncoder.

property logging_enabled: bool

Whether to log every cache hit, miss and invalidation, from LOGGING_ENABLED.

Return type:

bool

property verbose_logging: bool

Whether those logs include the arguments, from VERBOSE_LOGGING.

Return type:

bool

log_prefix(text: str, event: str = '') → str[source]

Format the start of a log message with the LOG_PREFIX callable.

Parameters:
  • text (str) – The unformatted text.

  • event (str) – "hit", "miss", "invalidate" or "".

Returns:

The formatted text.

Return type:

str

cache_results.conf.LOG_EVENTS = ('hit', 'miss', 'invalidate', '')

The events that a LOG_PREFIX callable is called with.

cache_results.conf.SETTING_NAME = 'CACHE_RESULTS'

The name of the Django setting that configures @cache_results.

cache_results.conf.cache_results_settings = <cache_results.conf.CacheResultsSettings object>

The CACHE_RESULTS setting, with its defaults.