core.orm.cache

Provides a simple and mostly transparent way of defining orm-cached properties on the application class.

For example:

from onegov.core import Framework
from onegov.core.orm import orm_cached

class App(Framework):

    @orm_cached(policy='on-table-change:users')
    def users(self):
        # ... fetch users from database

Properties defined in this way are accessible through the instance:

app.users

If there are any changes to the users table, the cache is removed. Since the cache is usually a shared redis instance, this works for multiple processes.

Attributes

Creator

_T

_QT

unset

Classes

OrmCacheApp

Integrates the orm cache handling into the application

OrmCacheDescriptor

The descriptor implements the protocol for fetching the objects

Functions

orm_cached(→ _OrmCacheDecorator)

The decorator use to setup the cache descriptor.

request_cached(→ _RequestCached[_FrameworkT, _T])

This is like a request scoped orm_cached().

Module Contents

core.orm.cache.Creator[source]
core.orm.cache._T[source]
core.orm.cache._QT[source]
core.orm.cache.unset[source]
class core.orm.cache.OrmCacheApp[source]

Integrates the orm cache handling into the application (i.e. onegov.core.framework.Framework).

In addition, the application needs to call setup_orm_cache() inside of :meth:onegov.server.application.Application.set_application_id to enable the cache evicition mechanism.

session_manager: core.orm.session_manager.SessionManager[source]
configure_orm_cache(**cfg: Any) None[source]
setup_orm_cache() None[source]

Sets up the event handlers for the change-detection.

descriptor_bound_orm_change_handler(descriptor: OrmCacheDescriptor[Any]) Callable[[str, Base], None][source]

Listens to changes to the database and evicts the cache if the policy demands it. Available policies:

  • policy=’on-table-change:table’: clears the cache if there’s a change on the given table

  • policy=lambda obj: …: clears the cache if the given policy function returns true (it receives the object which has changed)

property orm_cache_descriptors: Iterator[OrmCacheDescriptor[Any]][source]

Yields all orm cache descriptors installed on the class.

class core.orm.cache.OrmCacheDescriptor(cache_policy: CachePolicy, creator: Creator[Query[_QT]], by_role: bool = False)[source]
class core.orm.cache.OrmCacheDescriptor(cache_policy: CachePolicy, creator: Creator[_T], by_role: bool = False)

Bases: Generic[_T]

The descriptor implements the protocol for fetching the objects either from cache or creating them using the :param:creator.

You are not allowed to store ORM objects in this cache, since it leads to unpredictable results when attempting to merge the restored objects with the current session.

used_cache_keys: set[str][source]
cache_policy[source]
cache_key_prefix[source]
creator[source]
by_role = False[source]
cache_key(obj: OrmCacheApp | _HasApp) str[source]
assert_no_orm_objects(obj: object, depth: int = 0) None[source]

Ensures the object contains no ORM objects

create(instance: OrmCacheApp | _HasApp) _T[source]

Uses the creator to load the object to be cached.

Since the return value of the creator might not be something we want to cache, this function will turn some return values into something more useful (e.g. queries are completely fetched).

load(instance: OrmCacheApp | _HasApp) _T[source]

Loads the object from the database or cache.

__get__(instance: None, owner: type[Any]) Self[source]
__get__(instance: Any, owner: type[Any]) _T

Handles the object/cache access.

core.orm.cache.orm_cached(policy: CachePolicy, by_role: bool = False) _OrmCacheDecorator[source]

The decorator use to setup the cache descriptor.

See the onegov.core.orm.cache docs for usage.

core.orm.cache.request_cached(appmethod: Callable[[_FrameworkT], _T]) _RequestCached[_FrameworkT, _T][source]

This is like a request scoped orm_cached().

This may store ORM objects in contrast to orm_cached(), which should only be used to store other kinds of objects.