def install(cls): """Sets up the invalidation paths for the cached models in CACHETREE, and registers signal handlers for each of them. """ cls.INVALIDATION_PATHS = {} for app_label, model in get_cached_models(): cls.validate_lookups(model) cache_settings = get_cache_settings(model) attrs = cache_settings.get("prefetch") cls._add_invalidation_path(model, [], attrs) cls.connect_signals()
def _install(self): """Adds CacheManagerMixin to the default manager class for each model defined in CACHETREE, wraps the descriptor classes for many related objects to use the cache, and sets up invalidation. If CACHETREE_DISABLE is set to True, setup still adds the CacheManagerMixin to the appropriate classes, but makes get_cached an alias of get, allowing code that uses get_cached to continue to work. Invalidation setup is skipped. """ if self.installed: return if cachetree_settings.DISABLE: CacheManagerMixin.get_cached = lambda self, *args, **kwargs: self.get( *args, **kwargs) else: CacheManagerMixin.get_cached = self.method_cache.get( CacheManagerMixin).get("get_cached") for app_label, model in get_cached_models(): if CacheManagerMixin not in model._default_manager.__class__.__bases__: model._default_manager.__class__.__bases__ += ( CacheManagerMixin, ) for descriptor_cls in (ForeignRelatedObjectsDescriptor, ManyRelatedObjectsDescriptor, ReverseManyRelatedObjectsDescriptor): self.wrap_many_related_descriptor(descriptor_cls) self._install_auth_dependencies() if cachetree_settings.INVALIDATE and not cachetree_settings.DISABLE: Invalidator.install() self.installed = True