collective.redismemoize.adapter#

The cache adapter that stores values in Redis, or in RAM.

The cache adapter plone.memoize reads and writes through.

plone.memoize.volatile.cache needs a mapping with get, __getitem__ and __setitem__. RedisCacheAdapter provides them for one cached function, and decides per value where it goes:

  • plain data, within the size limit, goes to Redis with a TTL;

  • anything else goes to the stock RAM cache it wraps, and a refusal routes the whole function to RAM for the rest of the process;

  • a Redis failure is a miss, never an error, and opens the circuit breaker.

A read asks Redis first and the RAM cache after a Redis miss, because a value too large for Redis, or stored while the breaker was open, lives in RAM.

Values read from Redis are copies. A caller that mutates a cached value no longer mutates the cache, which the RAM cache allowed.

collective.redismemoize.adapter.hash_key(key)[source]#

Hash a cache key the way plone.memoize does.

Parameters:

key (str | bytes) -- The key volatile.cache built.

Returns:

Its SHA-1, in hex.

Return type:

str

class collective.redismemoize.adapter.RedisCacheAdapter[source]#

Bases: AbstractDict

Store one function's values in Redis, or in RAM when they cannot be.

Parameters:
  • globalkey -- Dotted name of the cached function.

  • settings -- The package settings.

  • ram -- The stock RAM adapter for the same function.

  • router -- Per-function routes and counters.

  • connection -- The Redis connection and its breaker.

  • ttl -- Lifetime of a stored value; settings.ttl when omitted.

__init__(globalkey, settings, ram, router, connection, ttl=None)[source]#
Parameters:
redis_key(key)[source]#

Return the Redis key of a cache key.

Parameters:

key (str | bytes) -- The key volatile.cache built.

Returns:

<prefix>:<function>:<sha1 of key>.

Return type:

str