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)