lck.django 0.8.5 documentation

This Page

lck.django.cache

A drop-in substitute for the default django.core.cache object. This implementation is using a modified mint cache approach based on http://www.djangosnippets.org/snippets/793/. Mint caching enables you to mitigate the so-called dogpile effect (also known as the thundering herd problem).

In short, it appears when generating a cached value takes long. In that situation the first request that encounters a cache miss, starts regenerating the value. Before it manages to finish and put the new value in the cache backend, other requests come in and also start the costly regeneration process (because they all saw a cache miss). This is a considerable waste of server resources and increases the amount of users affected by cache misses.

The solution implemented by this module is to notify a single request of the cache miss and then to keep serving stale values to subsequent requests until the notified request finishes updating the value.

Note

This implementation stores additional data along every cached value to enable the functionality described above. This means that trying to access those values using the regular django.core.cache will return a tuple instead of a bare value.

This implementation does not support specifying fallback values for the get() function since it would not notify client code that a value is stale and has to be updated. Consequently, the dogile effect would occur anyway.

Configured by three values in settings.py:

  • CACHE_DEFAULT_TIMEOUT - how long a set value should be considered valid (in seconds). After this period the value is considered stale, a single request starts to update it, and subsequent requests get the old value until the value gets updated. Default: 300 seconds.
  • CACHE_FILELOCK_PATH - path to a non-existant file which will work as an interprocess lock to make cache access atomic. Default: /tmp/langacore_django_cache.lock
  • CACHE_MINT_DELAY - an upper bound on how long a value should take to be generated (in seconds). This value is used for stale keys and is the real time after which the key is completely removed from the cache. Default: 30 seconds.

Functions

get(*args, **kwargs)

Get a value from the cache.

Parameters:
  • key – the key for which to return the value
  • invalidator – if the value was set with an invalidator, this parameter should contain a current value for it. If the stored value differs from the current, the cached value is considered invalid just like with a regular timeout.
set(*args, **kwargs)

Set a value in the cache.

Parameters:
  • key – the key under which to set the value
  • val – the value to set
  • invalidator – additional data that render a cached value invalid if different on cache.get()
  • timeout – how long should this value be valid, by default CACHE_DEFAULT_TIMEOUT
  • _is_stale – boolean, used internally to set a stale value in the cache back-end. Don’t use on your own.
delete(*args, **kwargs)

Removes a value from the cache.

Parameters:key – the key to delete from the cache