Lazy Cache

Django’s cache, imported only once Django is ready.

Importing django.core.cache while Django is still starting can leave it on an unexpected backend, such as a local-memory cache that silently persists nothing: sessions are lost, and cached results are recomputed in every process. lazy_cache has the api of django.core.cache.cache, and imports it only on first use, once Django’s apps are ready.

from cache_results import lazy_cache as cache

cache.set("my_key", "my_value", timeout=300)
cache.get("my_key")  # "my_value"

On first use it also checks the cache: it writes and reads a test key, and warns when the default cache is a local-memory or dummy backend, which is usually a misconfiguration in production.

class cache_results.lazy.LazyCache[source]

Bases: object

Django’s default cache, imported on first use.

Use the module’s lazy_cache instance. Every method passes its arguments to the method of the same name of django.core.cache.cache.

property ready: bool

Whether the cache can be used yet: True once Django’s apps are ready.

Until then, cache returns None rather than initialize Django’s cache prematurely, so callers that may run during startup check this first, as @cache_results does.

Return type:

bool

property cache

Import and return django.core.cache.cache, checking it on first use.

Returns:

The cache, or None before Django’s apps are ready.

Return type:

Optional[django.core.cache.backends.base.BaseCache]

get(key, default=None, version=None)[source]

Fetch a key, or default if it is not cached.

set(key, value, timeout=<object object>, version=None)[source]

Set a value.

Without timeout, the cache’s own timeout is used, and None never expires.

add(key, value, timeout=<object object>, version=None) → bool[source]

Set a value only if the key is not cached yet.

touch(key, timeout=<object object>, version=None) → bool[source]

Give a key a new timeout.

delete(key, version=None) → bool[source]

Delete a key.

has_key(key, version=None) → bool[source]

Whether a key is cached.

incr(key, delta: int = 1, version=None) → int[source]

Add delta to a cached number.

decr(key, delta: int = 1, version=None) → int[source]

Subtract delta from a cached number.

get_many(keys, version=None) → dict[source]

Fetch several keys, as a dict of those that are cached.

set_many(data: dict, timeout=<object object>, version=None) → list | None[source]

Set several values.

Returns what the backend does: Django’s own backends, the keys that could not be set.

delete_many(keys, version=None) → None[source]

Delete several keys.

incr_version(key, delta: int = 1, version=None) → int[source]

Move a key to a higher version.

decr_version(key, delta: int = 1, version=None) → int[source]

Move a key to a lower version.

clear() → None[source]

Delete every key in the cache, not only those of this application.

close(**kwargs) → None[source]

Close the connection to the cache.

cache_results.lazy.lazy_cache = <cache_results.lazy.LazyCache object>

Django’s default cache, imported on first use, with the api of django.core.cache.cache.