def wrapper(*args, **kwargs): cache_key = generate_cache_key(url, element, delinate) if refresh: unmemoize(url, element, delinate, delete_timeout) data = utils.deserialize_models(memcache.get(cache_key)) if flush: unmemoize(url, element, delinate, delete_timeout) elif data is None: data = func(*args, **kwargs) memcache.set(cache_key, utils.serialize_models(data), timeout) logging.debug("Memoizing: %s(%s)" % (cache_key, url)) return data
def get_by_key_name(cls, key_names, parent=None): # We need to perseve the return value if the input is a list # other wise we need to return just the entity itself. is_list = True # memcache.get_mutli() only accepts lists. if not isinstance(key_names, list): is_list = False key_names = [key_names] r = cls.__fill_blanks(key_names, memcache.get_multi(key_names)) r = utils.deserialize_models(r) if not [x for x in r if x is not None]: r = super(ModelCaching, cls).get_by_key_name(key_names, parent) if r: logging.info("CACHING GET") if isinstance(r, db.Model): #logging.info('CACHING: %s' % r.key_name) memcache.set(r.key_name, utils.serialize_models(r)) else: memcache.set_multi(dict([(x.key_name, utils.serialize_models(x)) for x in r if x])) return r if is_list else r[0]
def put(self, *args, **kwargs): """put(*args, **kwargs) -> None Set the object in the cache and clear the object's various caches before every put() call. Returns whatever put() returns. """ # We need to put() the instance to set the key before we can act on it. r = super(ModelCaching, self).put(*args, **kwargs) #logging.info("CACHING PUT") memcache.set(self.key_name, utils.serialize_models(self)) cache_keys = ['%s:%s' % (x, self.key_name) for x in self._memo_prefixes] memcache.delete_multi(cache_keys) return r