Source code for anyblok.model.cache
# This file is a part of the AnyBlok project
#
# Copyright (C) 2017 Jean-Sebastien SUZANNE <jssuzanne@anybox.fr>
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file,You can
# obtain one at http://mozilla.org/MPL/2.0/.
from anyblok.common import apply_cache
from .plugins import ModelPluginBase
[docs]class CachePlugin(ModelPluginBase):
def __init__(self, registry):
if not hasattr(registry, "caches"):
registry.caches = {}
super(CachePlugin, self).__init__(registry)
[docs] def insert_in_bases(
self, new_base, namespace, properties, transformation_properties
):
"""Create overload to define the cache from __depends__.
Because the cache is defined on the depend models and this namespace
does not exist in caches dict
:param new_base: the base to be put on front of all bases
:param namespace: the namespace of the model
:param properties: the properties declared in the model
:param transformation_properties: the properties of the model
"""
for dep in properties["__depends__"]:
if dep in self.registry.caches:
cache = self.registry.caches.setdefault(namespace, {})
for method_name, methods in self.registry.caches[dep].items():
entry = cache.setdefault(method_name, [])
entry.extend(methods)
return {}