def test_get_many(self): if not self._run_test(): return cache.set("hello1","world1",30) cache.set("hello2","world2",30) self._commit_and_clear_log() self.assertEquals(cache.get_many(['hello1','hello2']),{version_key('hello1'):'world1',version_key('hello2'):'world2'}) self.assertEquals(len(cache._logger.log), 1) self.assertEquals(cache.get_many(['hello1','hello2']),{version_key('hello1'):'world1',version_key('hello2'):'world2'}) self.assertEquals(len(cache._logger.log), 1)
def test_add(self): if not self._run_test(): return self.assertEquals(local.storage.get(version_key("hello")), None) cache.add("hello","world") self.assertEquals(len(cache._logger.log), 1) self.assertEquals(local.storage.get(version_key("hello")), "world") cache.add("hello","world1") self.assertEquals(local.storage.get(version_key("hello")), "world") self.assertEquals(len(cache._logger.log), 1) cache.close() self.assertEquals(len(cache._logger.log), 1)
def test_get(self): if not self._run_test(): return cache.set("hello","world",30) self._commit_and_clear_log() self.assertEquals(local.storage.get(version_key("hello")), None) self.assertEquals(cache.get("hello"),"world") self.assertEquals(local.storage.get(version_key("hello")), "world") self.assertEquals(len(cache._logger.log), 1) self.assertEquals(cache.get("hello"),"world") self.assertEquals(len(cache._logger.log), 1)
def load_cache_signals(sender, **kwargs): """On startup, sync signals with registered models""" from django.db import connection if not cache_signals.ready: # Have to load directly from db, because CacheBotSignals is not prepared yet cursor = connection.cursor() try: cursor.execute("SELECT * FROM %s" % CacheBotSignals._meta.db_table) except Exception, ex: # This should only happen on syncdb when CacheBot tables haven't been created yet, # but there's not really a good way to catch this error sql, references = connection.creation.sql_create_model(CacheBotSignals, no_style()) cursor.execute(sql[0]) cursor.execute("SELECT * FROM %s" % CacheBotSignals._meta.db_table) results = cursor.fetchall() tables = [r[1] for r in results] mapping = cache.get_many(tables) for r in results: key = version_key(".".join(("cachesignals", r[1]))) accessor_set = mapping.get(key) if accessor_set is None: accessor_set = set() accessor_set.add(r[2:5]) mapping[key] = accessor_set cache.set_many(mapping, CACHE_SECONDS) cache_signals.ready = True
def set_many(self, data, timeout=0): safe_data = {} for key, value in data.items(): key = version_key(key) if isinstance(value, unicode): value = value.encode('utf-8') safe_data[smart_str(key)] = value self._cache.set_multi(safe_data, timeout or self.default_timeout)
def get_cache_key(self, extra_args=''): """Cache key used to identify this query""" query, params = self.query.get_compiler(using=self.db).as_sql() query_string = (query % params).strip().encode("utf-8") base_key = md5_constructor('.'.join( (query_string, extra_args))).hexdigest() return version_key('.'.join( (self.model._meta.db_table, 'cachebot.results', base_key)))
def load_cache_signals(**kwargs): """On startup, sync signals with registered models""" if not cache_signals.ready: results = CacheBotSignals.objects.all() tables = [r.table_name for r in results] mapping = cache.get_many(tables) for result in results: key = version_key(u'.'.join(('cachesignals', result.table_name))) accessor_set = mapping.get(key) or set() accessor_set.add((result.accessor_path, result.lookup_type, result.exclude)) mapping[key] = accessor_set cache.set_many(mapping, 0) cache_signals.ready = True
def test_delete_many(self): if not self._run_test(): return cache.set_many({'hello1':'world1','hello2':'world2'},30) self._commit_and_clear_log() cache.delete_many(['hello1','hello2']) self.assertEquals(len(cache._logger.log), 0) cache.delete_many(['hello1','hello2']) self.assertEquals(len(cache._logger.log), 0) self.assertEquals(cache.get_many(['hello1','hello2']),{version_key('hello1'):None,version_key('hello2'):None}) self.assertEquals(len(cache._logger.log), 0) cache.close() self.assertEquals(len(cache._logger.log), 1)
def load_cache_signals(**kwargs): """On startup, sync signals with registered models""" if not cache_signals.ready: results = CacheBotSignals.objects.all() tables = [r.table_name for r in results] mapping = cache.get_many(tables) for result in results: key = version_key(u'.'.join(('cachesignals', result.table_name))) accessor_set = mapping.get(key) or set() accessor_set.add( (result.accessor_path, result.lookup_type, result.exclude)) mapping[key] = accessor_set cache.set_many(mapping, 0) cache_signals.ready = True
def get_invalidation_key(table_alias, accessor_path='', lookup_type='exact', negate=False, value='', save=True): """An invalidation key is associated with a set of cached queries. A blank accessor_path will create an invalidation key for this entire table instead of a specific row""" # punt on this problem for now if isinstance(value, QueryWrapper) or lookup_type != 'exact' or negate: value = '' if hasattr(value, '__iter__'): if len(value) == 1: value = value[0] else: value = '' #print save, table_alias,accessor_path,str(value) base_key = md5_constructor(u'.'.join(('cachebot:invalidation',accessor_path,unicode(value))).encode('utf-8')).hexdigest() return version_key('.'.join((table_alias,base_key)))
def get_many_by_key(cache_key_f, item_keys): """ For a series of item keys and a function that maps these keys to cache keys, get all the items from the cache if they are available there. Return a dictionary mapping the item keys to the objects retrieved from the cache. Any items not found in the cache are not returned. """ cache_key_to_item_key = {} for item_key in item_keys: cache_key = version_key(cache_key_f(item_key)) cache_key_to_item_key[cache_key] = item_key # request from cache from_cache = cache.get_many(cache_key_to_item_key.keys()) results = {} for cache_key, value in from_cache.iteritems(): item_key = cache_key_to_item_key[cache_key] results[item_key] = value return results
def get_invalidation_key(table_alias, accessor_path='', lookup_type='exact', negate=False, value=''): """ An invalidation key is associated with a set of cached queries. A blank accessor_path will create an invalidation key for this entire table instead of a specific row """ # punt on this problem for now if isinstance(value, QueryWrapper) or lookup_type != 'exact' or negate: value = '' if hasattr(value, '__iter__'): if len(value) == 1: value = value[0] else: value = '' base_key = md5_constructor('.'.join( (accessor_path, unicode(value))).encode('utf-8')).hexdigest() return version_key('.'.join( (table_alias, 'cachebot.invalidation', base_key)))
def set_many(self, data, timeout=0): for key, value in data.items(): key = version_key(key) self.set(key, value, timeout)
def get_cache_key(self, extra_args=''): """Cache key used to identify this query""" query, params = self.query.get_compiler(using=self.db).as_sql() query_string = (query % params).strip().encode("utf-8") base_key = md5_constructor('.'.join((query_string, extra_args))).hexdigest() return version_key('.'.join((self.model._meta.db_table, 'cachebot.results', base_key)))
def get_lookup_key(self, model_class): return version_key(".".join(("cachesignals", model_class._meta.db_table)))
def set_many(self, data, timeout=0): for key, value in data.items(): key = version_key(key) if isinstance(value, unicode): value = value.encode('utf-8') self.set(smart_str(key), value, timeout or self.default_timeout)
def get_lookup_key(self, model_class): return version_key('.'.join( ('cachesignals', model_class._meta.db_table)))
def set_many(self, mapping, timeout=None): return self._call( "set_multi", dict((smart_str(version_key(key)), val) for key, val in mapping.iteritems()), self.default_timeout if timeout is None else timeout, )
def get_cache_key(self, extra_args=''): """Cache key used to identify this query""" query, params = self.query.as_sql() query_string = (query % params).strip() base_key = md5_constructor('.'.join(('cachebot:result_key',str(self.__class__),query_string, extra_args))).hexdigest() return version_key('.'.join((self.model._meta.db_table,base_key)))
def get_lookup_key(self, model_class): return version_key('.'.join(('cachesignals', model_class._meta.db_table)))