edx_django_utils.cache package#

Subpackages#

Submodules#

edx_django_utils.cache.middleware module#

Caching utility middleware.

class edx_django_utils.cache.middleware.RequestCacheMiddleware(get_response=None)#

Bases: django.utils.deprecation.MiddlewareMixin

Middleware to clear the request cache as appropriate for new requests.

process_request(request)#

Clears the request cache before processing the request.

process_response(request, response)#

Clear the request cache after processing a response.

class edx_django_utils.cache.middleware.TieredCacheMiddleware(get_response=None)#

Bases: django.utils.deprecation.MiddlewareMixin

Middleware to store whether or not to force django cache misses.

process_request(request)#

Stores whether or not FORCE_CACHE_MISS_PARAM was supplied in the request.

edx_django_utils.cache.utils module#

Cache utilities.

class edx_django_utils.cache.utils.CachedResponse(is_found, key, value)#

Bases: object

Represents a cache response including is_found status and value.

get_value_or_default(default)#

Returns value for a cache hit, or the passed default for a cache miss.

This method is safe to use, even for a cache_miss.

WARNING: Never pass None as the default and then test the return value of this method. Use is_found instead for any checks.

exception edx_django_utils.cache.utils.CachedResponseError(message='CachedResponse was misused. Try the attributes is_found, value or key.')#

Bases: Exception

Error used when CachedResponse is misused.

USAGE_MESSAGE = 'CachedResponse was misused. Try the attributes is_found, value or key.'#
class edx_django_utils.cache.utils.RequestCache(namespace=None)#

Bases: object

A namespaced request cache for caching per-request data.

clear()#

Clears data for the namespaced request cache.

classmethod clear_all_namespaces()#

Clears the data for all namespaces.

property data#

Returns the namespaced cached key/value pairs as a dict.

delete(key)#

Deletes the cached value for the provided key.

Parameters

key (string) –

get_cached_response(key)#

Retrieves a CachedResponse for the provided key.

Parameters

key (string) –

Returns

A CachedResponse with is_found status and value.

set(key, value)#

Caches the value for the provided key.

Parameters
  • key (string) –

  • value (object) –

setdefault(key, value)#

Sets the value for the provided key if it has not yet been set.

Parameters
  • key (string) –

  • value (object) –

class edx_django_utils.cache.utils.TieredCache#

Bases: object

A two tiered caching object with a request cache backed by a django cache.

static dangerous_clear_all_tiers()#

This clears both the default request cache and the entire django backing cache.

Important: This should probably only be called for testing purposes.

TODO: Move CacheIsolationMixin from edx-platform to edx-django-utils and kill this method.

static delete_all_tiers(key)#

Deletes the cached value for the provided key in both the request cache and the django cache.

Parameters

key (string) –

classmethod get_cached_response(key)#

Retrieves a CachedResponse for the provided key.

Parameters

key (string) –

Returns

A CachedResponse with is_found status and value.

static set_all_tiers(key, value, django_cache_timeout=<object object>)#

Caches the value for the provided key in both the request cache and the django cache.

Parameters
  • key (string) –

  • value (object) –

  • django_cache_timeout (int) – (Optional) Timeout used to determine if and for how long to cache in the django cache. A timeout of 0 will skip the django cache. If timeout is provided, use that timeout for the key; otherwise use the default cache timeout.

edx_django_utils.cache.utils.get_cache_key(**kwargs)#

Get MD5 encoded cache key for given arguments.

Note: We convert keyword arguments to their string form to build the cache key. So do not pass arguments that can’t be converted to strings. Also, don’t pass arguments that may not consistently be converted to the same string, like an unsorted dict. Here is the format of key before MD5 encryption.

key1:value1__key2:value2 …

Example

>>> get_cache_key(site_domain="example.com", resource="catalogs")
# Here is key format for above call
# "site_domain:example.com__resource:catalogs"
a54349175618ff1659dee0978e3149ca
Parameters

**kwargs – Key word arguments that need to be present in cache key.

Returns

An MD5 encoded key uniquely identified by the key word arguments.

Module contents#

Cache utilities public api

See README.rst for details.