async def set(self, key, value): try: size = self.get_size(value) self._memory_cache.set(key, value, size) if self._obj_driver is not None: await self._obj_driver.set(CACHE_PREFIX + key, serialize.dumps(value), expire=self._settings.get( 'ttl', 3600)) logger.info('set {} in cache'.format(key)) except Exception: logger.warning('Error setting cache value', exc_info=True)
async def set(self, keys, value, ttl=None): if not isinstance(keys, list): keys = [keys] size = self.get_size(value) for key in keys: try: self._memory_cache.set(key, value, size) if ttl is None: ttl = self._settings.get("ttl", 3600) if self._obj_driver is not None: stored_value = serialize.dumps(value) await self._obj_driver.set(CACHE_PREFIX + key, stored_value, expire=ttl) logger.debug("set {} in cache".format(key)) except Exception: logger.warning("Error setting cache value", exc_info=True) size = 0 # additional keys to set have 0 size in cache
async def test_subscriber_ignores_trsn_on_invalidate(redis_container, guillotina_main): util = get_utility(ICacheUtility) await util.initialize() assert util.initialized assert util._obj_driver is not None assert util._subscriber is not None trns = mocks.MockTransaction(mocks.MockTransactionManager()) trns.added = trns.deleted = {} content = create_content() trns.modified = {content.__uuid__: content} rcache = BasicCache(trns) await rcache.clear() driver = await resolve_dotted_name("guillotina.contrib.redis").get_driver() await rcache.set("foobar", oid=content.__uuid__) assert serialize.loads(await driver.get(CACHE_PREFIX + "root-" + content.__uuid__)) == "foobar" assert util._memory_cache.get("root-" + content.__uuid__) == "foobar" assert await rcache.get(oid=content.__uuid__) == "foobar" assert "root-" + content.__uuid__ in util._memory_cache util.ignore_tid(5555) await driver.publish( app_settings["cache"]["updates_channel"], pickle.dumps({ "data": serialize.dumps({ "tid": 5555, "keys": ["root-" + content.__uuid__] }), "ruid": "nonce" }), ) await asyncio.sleep(1) # should be enough for pub/sub to finish # should still be there because we set to ignore this tid assert "root-" + content.__uuid__ in util._memory_cache # tid should also now be removed from ignored list assert 5555 not in util._ignored_tids
async def test_subscriber_invalidates(redis_container, guillotina_main, loop): util = get_utility(ICacheUtility) await util.initialize() assert util.initialized assert util._obj_driver is not None assert util._subscriber is not None trns = mocks.MockTransaction(mocks.MockTransactionManager()) trns.added = trns.deleted = {} content = create_content() trns.modified = {content.__uuid__: content} rcache = BasicCache(trns) await rcache.clear() await rcache.set('foobar', oid=content.__uuid__) driver = await resolve_dotted_name('guillotina.contrib.redis').get_driver() assert serialize.loads(await driver.get(CACHE_PREFIX + 'root-' + content.__uuid__)) == "foobar" assert util._memory_cache.get('root-' + content.__uuid__) == 'foobar' assert await rcache.get(oid=content.__uuid__) == 'foobar' assert 'root-' + content.__uuid__ in util._memory_cache await driver.publish( app_settings['cache']['updates_channel'], pickle.dumps({ 'data': serialize.dumps({ 'tid': 32423, 'keys': ['root-' + content.__uuid__] }), 'ruid': 'nonce' })) await asyncio.sleep(1) # should be enough for pub/sub to finish assert 'root-' + content.__uuid__ not in util._memory_cache