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 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)
def register_test(key, object, content_path): CACHE_DELETER.register_pk(object, key)