def render(self): " Cached wrapper around self._render(). " if getattr(settings, 'DOUBLE_RENDER', False) and self.can_double_render: if 'SECOND_RENDER' not in self._context: return self.double_render() key = self.get_cache_key() rend = cache.get(key) if rend is None: rend = self._render() cache.set(key, rend, core_settings.CACHE_TIMEOUT) for model, test in self.get_cache_tests(): CACHE_DELETER.register_test(model, test, key) CACHE_DELETER.register_pk(self.obj, key) return rend
def get_cached_object(model, **kwargs): """ Return a cached object. If the object does not exist in the cache, create it and register it for invalidation if the object is updated (check via _get_pk_val(). Params: model - Model class ContentType instance representing the model's class **kwargs - lookup parameters for content_type.get_object_for_this_type and for key creation Throws: model.DoesNotExist is propagated from content_type.get_object_for_this_type """ if isinstance(model, ContentType): model = model.model_class() key = _get_key(KEY_FORMAT_OBJECT, model, kwargs) obj = cache.get(key) if obj is None: obj = model._default_manager.get(**kwargs) cache.set(key, obj, CACHE_TIMEOUT) CACHE_DELETER.register_pk(obj, key) return obj
def get_cached_list(model, *args, **kwargs): """ Return a cached list. If the list does not exist in the cache, create it and register it for invalidation if any object from the list is updated (check via _get_pk_val()). Params: model - Model class ContentType instance representing the model's class **kwargs - lookup parameters for content_type.get_object_for_this_type and for key creation """ if isinstance(model, ContentType): model = model.model_class() key = _get_key(KEY_FORMAT_LIST, model, kwargs) l = cache.get(key) if l is None: log.debug('get_cached_list(model=%s), object not cached.' % str(model)) l = list(model._default_manager.filter(*args, **kwargs)) cache.set(key, l, CACHE_TIMEOUT) for o in l: CACHE_DELETER.register_pk(o, key) #CACHE_DELETER.register_test(model, lambda x: model._default_manager.filter(**kwargs).filter(pk=x._get_pk_val()) == 1, key) return l
def invalidate_cache(key, template_name, template_dirs=None): from ella.db_templates.models import DbTemplate if DbTemplate._meta.installed: CACHE_DELETER.register_test(DbTemplate, "name:%s" % template_name, key)
# push context stack context.push() context[core_settings.BOX_INFO] = box_key # render the box result = box.render() # restore the context context.pop() # record parent box dependecy on child box or cached full-page on box if not (core_settings.DOUBLE_RENDER and box.can_double_render) and (core_settings.BOX_INFO in context or core_settings.ECACHE_INFO in context): if core_settings.BOX_INFO in context: source_key = context[core_settings.BOX_INFO] elif core_settings.ECACHE_INFO in context: source_key = context[core_settings.ECACHE_INFO] CACHE_DELETER.register_dependency(source_key, box_key) return result @register.tag('box') def do_box(parser, token): """ Tag Node representing our idea of a reusable box. It can handle multiple parameters in its body which will then be accessible via ``{{ box.params }}`` in the template being rendered. .. note:: The inside of the box will be rendered only when redering the box in current context and the ``object`` template variable will be present and set to the target of the box.
def invalidate_listing(key, self, *args, **kwargs): CACHE_DELETER.register_test(self.model, '', key)
def register_test(key, object, content_path): CACHE_DELETER.register_pk(object, key)
def invalidate_cache(key, self, object, **kwargs): target_ct = ContentType.objects.get_for_model(object) CACHE_DELETER.register_test(Comment, "target_id:%s;target_ct_id:%s" % (object.pk, target_ct.pk) , key)
def gallery_cache_invalidator(key, gallery, *args, **kwargs): """Registers gallery cache invalidator test in the cache system.""" CACHE_DELETER.register_pk(gallery, key) CACHE_DELETER.register_test(GalleryItem, 'gallery_id:%s' % gallery.pk, key)
context[core_settings.BOX_INFO] = box_key # render the box result = box.render() # restore the context context.pop() # record parent box dependecy on child box or cached full-page on box if not (core_settings.DOUBLE_RENDER and box.can_double_render) and ( core_settings.BOX_INFO in context or core_settings.ECACHE_INFO in context): if core_settings.BOX_INFO in context: source_key = context[core_settings.BOX_INFO] elif core_settings.ECACHE_INFO in context: source_key = context[core_settings.ECACHE_INFO] CACHE_DELETER.register_dependency(source_key, box_key) return result @register.tag('box') def do_box(parser, token): """ Tag Node representing our idea of a reusable box. It can handle multiple parameters in its body which will then be accessible via ``{{ box.params }}`` in the template being rendered. .. note:: The inside of the box will be rendered only when redering the box in current context and the ``object`` template variable will be present and set to the target of the box.