def set(key, value): """Set value in instance cache and a compressed version in memcache. BingoCache is always only stored in instance cache for up to 1 minute. """ instance_cache.set(key, value, expiry=CacheLayers.INSTANCE_SECONDS) memcache.set(key, CacheLayers.compress(value)) logging.info("Set BingoCache in instance cache and memcache")
def fill_request_cache(): """Load BingoCache/BingoIdentityCache from instance cache/memcache. This loads the shared BingoCache and the individual BingoIdentityCache for the current request's bingo identity and stores them both in the request cache. """ if not request_cache.cache.get("bingo_request_cache_filled"): # Assume that we're going to grab both BingoCache and # BingoIdentityCache from memcache memcache_keys = [ BingoCache.CACHE_KEY, BingoIdentityCache.key_for_identity(identity()) ] # Try to grab BingoCache from instance cache bingo_instance = instance_cache.get(BingoCache.CACHE_KEY) if bingo_instance: # If successful, use instance cached version... request_cache.cache[BingoCache.CACHE_KEY] = bingo_instance # ...and don't load BingoCache from memcache memcache_keys.remove(BingoCache.CACHE_KEY) # Load necessary caches from memcache dict_memcache = memcache.get_multi(memcache_keys) # Decompress BingoCache if we loaded it from memcache if BingoCache.CACHE_KEY in dict_memcache: dict_memcache[BingoCache.CACHE_KEY] = CacheLayers.decompress( dict_memcache[BingoCache.CACHE_KEY]) # Update request cache with values loaded from memcache request_cache.cache.update(dict_memcache) if not bingo_instance: # And if BingoCache wasn't in the instance cache already, store # it with a 1-minute expiry instance_cache.set(BingoCache.CACHE_KEY, request_cache.cache.get( BingoCache.CACHE_KEY), expiry=CacheLayers.INSTANCE_SECONDS) request_cache.cache["bingo_request_cache_filled"] = True
def fill_request_cache(): """Load BingoCache/BingoIdentityCache from instance cache/memcache. This loads the shared BingoCache and the individual BingoIdentityCache for the current request's bingo identity and stores them both in the request cache. """ if not request_cache.cache.get("bingo_request_cache_filled"): # Assume that we're going to grab both BingoCache and # BingoIdentityCache from memcache memcache_keys = [ BingoCache.CACHE_KEY, BingoIdentityCache.key_for_identity(identity()) ] # Try to grab BingoCache from instance cache bingo_instance = instance_cache.get(BingoCache.CACHE_KEY) if bingo_instance: # If successful, use instance cached version... request_cache.cache[BingoCache.CACHE_KEY] = bingo_instance # ...and don't load BingoCache from memcache memcache_keys.remove(BingoCache.CACHE_KEY) # Load necessary caches from memcache dict_memcache = memcache.get_multi(memcache_keys) # Decompress BingoCache if we loaded it from memcache if BingoCache.CACHE_KEY in dict_memcache: dict_memcache[BingoCache.CACHE_KEY] = CacheLayers.decompress( dict_memcache[BingoCache.CACHE_KEY]) # Update request cache with values loaded from memcache request_cache.cache.update(dict_memcache) if not bingo_instance: # And if BingoCache wasn't in the instance cache already, store # it with a 1-minute expiry instance_cache.set(BingoCache.CACHE_KEY, request_cache.cache.get(BingoCache.CACHE_KEY), expiry=CacheLayers.INSTANCE_SECONDS) request_cache.cache["bingo_request_cache_filled"] = True
def test_no_expiry_should_last_forever(self): instance_cache.set('foo', 'bar', expiry=None) # A month passes - what incredible up time we have! month_in_secs = 60 * 60 * 24 * 31 self.adjust_time(delta_in_seconds=month_in_secs) self.assertEquals('bar', instance_cache.get('foo'))
def test_expiry_works_as_expected(self): instance_cache.set('foo', 'bar', expiry=60) self.assertEquals('bar', instance_cache.get('foo')) self.adjust_time(delta_in_seconds=61) self.assertEquals(None, instance_cache.get('foo'))