def _uninstall(self): """Uninstalls by restoring the descriptor classes to their original state, disconnecting invalidation signal handlers if necessary, and aliasing CacheManagerMixin.get_cached to get. (Aliases rather than removes CacheManagerMixin in order not to break code that uses it.) """ if not self.installed: return CacheManagerMixin.get_cached = lambda self, *args, **kwargs: self.get( *args, **kwargs) for descriptor_cls in (ForeignRelatedObjectsDescriptor, ManyRelatedObjectsDescriptor, ReverseManyRelatedObjectsDescriptor): descriptor_cls.__get__ = self.method_cache.get(descriptor_cls).get( "__get__") if cachetree_settings.INVALIDATE and not cachetree_settings.DISABLE: Invalidator.uninstall() self.installed = False
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