示例#1
0
async def test_subscriber_invalidates(redis_container, dummy_guillotina, loop):
    await cache.close_redis_pool()
    trns = mocks.MockTransaction(mocks.MockTransactionManager())
    trns.added = trns.deleted = {}
    content = create_content()
    trns.modified = {content._p_oid: content}
    rcache = RedisCache(trns, loop=loop)
    await rcache.clear()

    await rcache.set('foobar', oid=content._p_oid)
    assert serialize.loads(
        await rcache._redis.get(
            CACHE_PREFIX + 'root-' + content._p_oid)) == "foobar"
    assert rcache._memory_cache.get(
        'root-' + content._p_oid) == 'foobar'
    assert await rcache.get(oid=content._p_oid) == 'foobar'

    assert 'root-' + content._p_oid in rcache._memory_cache

    await rcache._redis.publish(
        app_settings['redis']['updates_channel'], serialize.dumps({
            'tid': 32423,
            'keys': ['root-' + content._p_oid]
        }))
    await asyncio.sleep(1)  # should be enough for pub/sub to finish
    assert 'root-' + content._p_oid not in rcache._memory_cache

    await cache.close_redis_pool()
示例#2
0
async def test_subscriber_ignores_trsn_on_invalidate(
        redis_container, dummy_guillotina, loop):
    await cache.close_redis_pool()
    trns = mocks.MockTransaction(mocks.MockTransactionManager())
    trns.added = trns.deleted = {}
    content = create_content()
    trns.modified = {content._p_oid: content}
    rcache = RedisCache(trns, loop=loop)
    await rcache.clear()

    await rcache.set('foobar', oid=content._p_oid)
    assert serialize.loads(
        await rcache._redis.get(
            CACHE_PREFIX + 'root-' + content._p_oid)) == "foobar"
    assert rcache._memory_cache.get('root-' + content._p_oid) == 'foobar'
    assert await rcache.get(oid=content._p_oid) == 'foobar'

    assert 'root-' + content._p_oid in rcache._memory_cache

    utility = getUtility(IRedisChannelUtility)
    utility.ignore_tid(5555)

    await rcache._redis.publish(
        app_settings['redis']['updates_channel'], serialize.dumps({
            'tid': 5555,
            'keys': ['root-' + content._p_oid]
        }))
    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._p_oid in rcache._memory_cache
    # tid should also now be removed from ignored list
    assert 5555 not in utility._ignored_tids

    await cache.close_redis_pool()
 async def set(self, value, **kwargs):
     key = self.get_key(**kwargs)
     try:
         conn = await self.get_redis()
         self._memory_cache[key] = value
         await conn.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)
示例#4
0
async def runit():
    print(f'Test content serialization')
    start = time.time()
    for _ in range(ITERATIONS):
        blah = serialize.dumps({
            'dlsfkds': 'dslfkdsf',
            'dslfks': 'sdlfkjds',
            'state': b'X' * ITERATIONS
        })
        serialize.loads(blah)
    end = time.time()
    print(f'Done with {ITERATIONS} in {end - start} seconds')
    async def _synchronize(self):
        '''
        publish cache changes on redis
        '''
        push = {}
        for obj, pickled in self._stored_objects:
            val = {
                'state': pickled,
                'zoid': obj._p_oid,
                'tid': obj._p_serial,
                'id': obj.__name__
            }
            if obj.__of__:
                ob_key = self.get_key(oid=obj.__of__,
                                      id=obj.__name__,
                                      variant='annotation')
                await self.set(val,
                               oid=obj.__of__,
                               id=obj.__name__,
                               variant='annotation')
            else:
                ob_key = self.get_key(container=obj.__parent__,
                                      id=obj.__name__)
                await self.set(val, container=obj.__parent__, id=obj.__name__)

            if ob_key in self._keys_to_publish:
                self._keys_to_publish.remove(ob_key)
            push[ob_key] = val

        channel_utility = getUtility(IRedisChannelUtility)
        channel_utility.ignore_tid(self._transaction._tid)
        await self._redis.publish(
            self._settings['updates_channel'],
            serialize.dumps({
                'tid': self._transaction._tid,
                'keys': self._keys_to_publish,
                'push': push
            }))

        self._keys_to_publish = []
        self._stored_objects = []