core.cache

Provides caching methods for onegov.core.

Onegov.core uses dogpile for caching: https://dogpilecache.readthedocs.org/

Unlike dogpile onegov.core does not provide a global region however. The cache is available through the app:

request.app.cache.set('key', 'value')

Global caches in a multi-tennant application are a security vulnerability waiting to be discovered. Therefore we do not do that!

This means that this won’t be available:

from x import cache
@cache.cache_on_arguments()
def my_function():
    return 'foobar'

Syntactic sugar like this will be provided through decorators inside this module in the future. For example, we could write one that is usable on all morepath views:

@App.view(...)
@cache.view()
def my_view():
    return '<html>...'

But no such method exists yet.

Currently there is one cache per app that never expires (though values will eventually be discarded by redis if the cache is full).

Attributes

_F

keys

flush

Classes

RedisCacheRegion

A slightly more specific CacheRegion that will be configured

Functions

instance_lru_cache(…)

Least-recently-used cache decorator for class methods.

dill_serialize(→ bytes)

dill_deserialize(→ Any)

get(→ RedisCacheRegion)

get_pool(→ redis.ConnectionPool)

Module Contents

core.cache._F[source]
core.cache.instance_lru_cache(*, maxsize: int | None = ...) Callable[[_F], _F][source]
core.cache.instance_lru_cache(method: _F, *, maxsize: int | None = ...) _F

Least-recently-used cache decorator for class methods.

The cache follows the lifetime of an object (it is stored on the object, not on the class) and can be used on unhashable objects.

This is a wrapper around functools.lru_cache which prevents memory leaks when using LRU cache within classes.

https://stackoverflow.com/a/71663059

core.cache.dill_serialize(value: Any) bytes[source]
core.cache.dill_deserialize(value: bytes | NoValue) Any[source]
class core.cache.RedisCacheRegion(namespace: str, expiration_time: float, redis_url: str)[source]

Bases: dogpile.cache.CacheRegion

A slightly more specific CacheRegion that will be configured to a single non-clustered Redis backend with name-mangling based on a given namespace as well as a couple of additional convenience methods specific to Redis.

It will use dill to serialize/deserialize values.

namespace[source]
key_mangler(key: str) bytes[source]
keys() list[str][source]
flush() int[source]
core.cache.keys[source]
core.cache.flush[source]
core.cache.get(namespace: str, expiration_time: float, redis_url: str) RedisCacheRegion[source]
core.cache.get_pool(redis_url: str) redis.ConnectionPool[source]