Example #1
0
async def test_set_none_bcast(create_cache):
    cachef_local = Memory()
    cachef = await create_cache(cachef_local)
    caches_local = Memory()
    caches = await create_cache(caches_local)

    await cachef.set("key", None, expire=0.1)
    await asyncio.sleep(0.01)  # skip init signal about invalidation
    assert await cachef_local.exists("key")
    assert await cachef.exists("key")

    assert await caches.get("key") is None
    assert await caches.exists("key")
    assert await caches_local.exists("key")
Example #2
0
async def test_del_bcast(create_cache):
    cachef_local = Memory()
    cachef = await create_cache(BcastClientSide, cachef_local)
    caches_local = Memory()
    caches = await create_cache(BcastClientSide, caches_local)

    await cachef.set("key", b"value")
    await asyncio.sleep(0.05)  # skip init signal about invalidation

    assert await cachef.get("key") == b"value"
    assert await caches.get("key") == b"value"
    await cachef.delete("key")
    await asyncio.sleep(0.05)  # skip init signal about invalidation
    assert await caches.get("key") is None
Example #3
0
async def test_simple_cmd_bcast(create_cache):
    local = Memory()
    cache = await create_cache(BcastClientSide, local)

    await cache.set("key:1", "test", 1)
    await asyncio.sleep(0.1)  # skip init signal about invalidation
    assert await cache.get("key:1") == "test"

    await cache.incr("key:2")
    assert await cache.get("key:2") == 1
    await cache.delete("key:2")
    assert await cache.get("key:2") is None
    assert await local.get("key:2") is None

    assert await cache.get("key:1") == "test"
    assert await local.get("key:1") == "test"
    await cache.expire("key:1", 3)
    assert await cache.get_expire("key:1") > 0
    assert await local.get_expire("key:1") > 0

    assert await cache.delete_match("key:*")
    assert await cache.get("key:1") is None
    assert await local.get("key:1") is None
    await cache.clear()
    cache.close()
Example #4
0
async def _cache():
    if os.environ.get("USE_REDIS"):
        redis = Redis("redis://", hash_key=None)
        await redis.init()
        await redis.clear()
        return redis
    return Memory()
Example #5
0
async def test_set_get_bcast(create_cache):
    cachef_local = Memory()
    cachef = await create_cache(BcastClientSide, cachef_local)
    caches_local = Memory()
    caches = await create_cache(BcastClientSide, caches_local)

    await cachef.set("key", b"value", expire=0.1)
    await asyncio.sleep(0.01)  # skip init signal about invalidation
    assert await cachef.get("key") == b"value"
    assert await caches.get("key") == b"value"
    assert await cachef_local.get("key") == b"value"
    assert await caches_local.get("key") == b"value"
    await asyncio.sleep(0.2)
    assert await cachef_local.get("key") is None
    assert await caches_local.get("key") is None

    assert await caches.get("key") is None
Example #6
0
async def _backend():
    if REDIS_TESTS:
        redis = Redis("redis://", hash_key=None)
        await redis.init()
        await redis.clear()
        return redis
    _cache = Memory()
    await _cache.init()
    return _cache
Example #7
0
async def test_rewrite_bcast(create_cache):
    cachef_local = Memory()
    cachef = await create_cache(cachef_local)
    caches_local = Memory()
    caches = await create_cache(caches_local)

    await cachef.set("key", b"value")
    await asyncio.sleep(0.05)  # skip init signal about invalidation

    assert await cachef.get("key") == b"value"
    assert await caches.get("key") == b"value"

    await caches.set("key", b"new", expire=0.1)
    await asyncio.sleep(0.05)  # skip init signal about invalidation

    assert await cachef.get("key") == b"new"

    await asyncio.sleep(0.15)
    assert await caches.get("key") is None
    assert await cachef.get("key") is None
Example #8
0
async def test_auto_init(cache):
    target = Mock(wraps=Memory())
    init = False

    def set_init():
        async def _set():
            nonlocal init
            await asyncio.sleep(0.01)
            init = True

        return _set()

    type(target).is_init = PropertyMock(side_effect=lambda: init)
    target.init.side_effect = set_init
    cache._backends[""] = (target, (_create_auto_init(), ))
    await asyncio.gather(cache.ping(), cache.ping(), cache.get("test"))
    target.init.assert_called_once()
Example #9
0
async def test_simple_cmd_bcast_many(create_cache):
    local = Memory()
    cache = await create_cache(local)
    await cache.set("key:1", "test")
    assert await cache.get("key:1") == "test"
    assert await local.get("key:1") == "test"

    assert await cache.get_many("key:1", "key:2") == ("test", None)

    async for key in cache.scan("key:*"):
        assert key == "key:1"
        break
    else:
        assert False

    async for key, value in cache.get_match("key:*"):
        assert key == "key:1"
        assert value == "test"
        break
    else:
        assert False

    await local.clear()

    assert await cache.get_many("key:1", "key:2") == ("test", None)

    async for key in cache.scan("key:*"):
        assert key == "key:1"

    async for key, value in cache.get_match("key:*"):
        assert key == "key:1"
        assert value == "test"

    assert await local.get("key:1") == "test"

    assert await cache.delete_match("key:*")
    assert await cache.get("key:1") is None
    assert await local.get("key:1") is None

    async for _ in cache.scan("key:*"):
        assert False

    async for _ in cache.get_match("key:*"):
        assert False
Example #10
0
async def test_lru():
    cache = Memory(size=10)
    # full cache
    for i in range(10):
        await cache.set(f"key:{i}", i)

    # use only 5 first
    for i in range(5):
        await cache.get(f"key:{i}")

    # add 5 more keys
    for i in range(5):
        await cache.set(f"key:{i}:new", i)

    assert len(cache.store) == 10

    for i in range(5):
        assert await cache.get(f"key:{i}") == i

    for i in range(6, 10):
        assert await cache.get(f"key:{i}") == None
Example #11
0
async def test_simple_cmd_bcast(create_cache):
    local = Memory()
    cache = await create_cache(local)

    await cache.incr("key:2")
    assert await cache.get("key:2") == 1
    await cache.delete("key:2")
    assert await cache.get("key:2") is None
    assert await local.get("key:2") is None

    await cache.set("key:1", "test", 10)
    assert await cache.get("key:1") == "test"
    assert await local.get("key:1") == "test"
    await cache.expire("key:1", 100)
    assert await cache.get_expire("key:1") > 10
    assert await local.get_expire("key:1") > 10
    assert await cache.get("key:1") == "test"
    assert await local.get("key:1") == "test"

    await cache.clear()
    cache.close()
Example #12
0
 async def call(cache_class, local_cache=Memory()):
     redis = cache_class("redis://", hash_key=None, local_cache=local_cache)
     await redis.init()
     await redis.clear()
     return redis
Example #13
0
async def test_auto_init(cache):
    target = Memory()
    cache._backends[""] = target, (_auto_init, )
    assert not target.is_init
    assert b"PONG" == await cache.ping()
    assert target.is_init
Example #14
0
async def rate_limit():
    return partial(_rate_limit, backend=Memory())