API Usage

Standard Django Cache API

get(self, key[, default=None]):

Retrieves a value from the cache.

Parameters:
  • key – Location of the value
  • default – Value to return if key does not exist in cache.
Return type:

Value that was cached.

add(self, key, value[, timeout=DEFAULT_TIMEOUT]):

Add a value to the cache, failing if the key already exists.

Parameters:
  • key – Location of the value
  • value – Value to cache
  • timeout (Number of seconds or DEFAULT_TIMEOUT) – Number of seconds to hold value in cache.
Return type:

True if object was added and False if it already exists.

set(self, key, value, timeout=DEFAULT_TIMEOUT):

Sets a value to the cache, regardless of whether it exists.

If timeout == None, then cache is set indefinitely. Otherwise, timeout defaults to the defined DEFAULT_TIMEOUT.

Parameters:
  • key – Location of the value
  • value – Value to cache
  • timeout (Number of seconds or DEFAULT_TIMEOUT) – Number of seconds to hold value in cache.
delete(self, key):

Removes a key from the cache

Parameters:key – Location of the value
delete_many(self, keys[, version=None]):

Removes multiple keys at once.

Parameters:
  • key – Location of the value
  • version – Version of keys
clear(self[, version=None]):

Flushes the cache. If version is provided, all keys under the version number will be deleted. Otherwise, all keys will be flushed.

Parameters:version – Version of keys
get_many(self, keys[, version=None]):

Retrieves many keys at once.

Parameters:keys – an iterable of keys to retrieve.
Return type:Dict of keys mapping to their values.
set_many(self, data[, timeout=None, version=None]):

Set many values in the cache at once from a dict of key/value pairs. This is much more efficient than calling set() multiple times and is atomic.

Parameters:
  • data – dict of key/value pairs to cache.
  • timeout (Number of seconds or None) – Number of seconds to hold value in cache.
incr(self, key[, delta=1]):

Add delta to value in the cache. If the key does not exist, raise a ValueError exception.

Parameters:
  • key – Location of the value
  • delta (Integer) – Integer used to increment a value.
incr_version(self, key[, delta=1, version=None]):

Adds delta to the cache version for the supplied key. Returns the new version.

Parameters:
  • key – Location of the value
  • delta (Integer) – Integer used to increment a value.
  • version (Integer or None) – Version of key

Cache Methods Provided by django-redis-cache

has_key(self, key):

Returns True if the key is in the cache and has not expired.

Parameters:key – Location of the value
Return type:bool
ttl(self, key):

Returns the ‘time-to-live’ of a key. If the key is not volatile, i.e. it has not set an expiration, then the value returned is None. Otherwise, the value is the number of seconds remaining. If the key does not exist, 0 is returned.

Parameters:key – Location of the value
Return type:Integer or None
delete_pattern(pattern[, version=None]):

Deletes keys matching the glob-style pattern provided.

Parameters:
  • pattern – Glob-style pattern used to select keys to delete.
  • version – Version of the keys
get_or_set(self, key, func[, timeout=None]):

Retrieves a key value from the cache and sets the value if it does not exist.

Parameters:
  • key – Location of the value
  • func – Callable used to set the value if key does not exist.
  • timeout (Number of seconds or None) – Number of seconds to hold value in cache.
reinsert_keys(self):

Helper function to reinsert keys using a different pickle protocol version.

persist(self, key):

Removes the timeout on a key.

Equivalent to setting a timeout of None in a set command. :param key: Location of the value :rtype: bool

expire(self, key, timeout):

Set the expire time on a key

Parameters:key – Location of the value
Return type:bool