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

_M

_T

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.

Module Contents

core.orm.cache._M[source]
core.orm.cache._T[source]
class core.orm.cache.OrmCacheApp[source]

Integrates the orm cache handling into the application (i.e. :class:`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.

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[_T]])[source]
class core.orm.cache.OrmCacheDescriptor(cache_policy: CachePolicy, creator: Creator[_T])

Bases: Generic[_T]

The descriptor implements the protocol for fetching the objects either from cache or from the database (through the handler).

create(app: OrmCacheApp) _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).

merge(session: sqlalchemy.orm.Session, obj: _M) _M[source]

Merges the given obj into the given session, if this is possible.

That is it acts like more forgiving session.merge().

requires_merge(obj: core.orm.Base) bool[source]

Returns true if the given object requires a merge, which is the case if the object is detached.

load(app: OrmCacheApp) _T[source]

Loads the object from the database or cache.

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

Handles the object/cache access.

core.orm.cache.orm_cached(policy: CachePolicy) _OrmCacheDecorator[source]

The decorator use to setup the cache descriptor.

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