1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import asyncio
from typing import Callable
from async_lru import alru_cache
async def test_ttl_infinite_cache(check_lru: Callable[..., None]) -> None:
@alru_cache(maxsize=None, ttl=0.1)
async def coro(val: int) -> int:
return val
assert await coro(1) == 1
check_lru(coro, hits=0, misses=1, cache=1, tasks=0, maxsize=None)
await asyncio.sleep(0.0)
assert await coro(1) == 1
check_lru(coro, hits=1, misses=1, cache=1, tasks=0, maxsize=None)
await asyncio.sleep(0.2)
# cache is clear after ttl expires
check_lru(coro, hits=1, misses=1, cache=0, tasks=0, maxsize=None)
assert await coro(1) == 1
check_lru(coro, hits=1, misses=2, cache=1, tasks=0, maxsize=None)
async def test_ttl_limited_cache(check_lru: Callable[..., None]) -> None:
@alru_cache(maxsize=1, ttl=0.1)
async def coro(val: int) -> int:
return val
assert await coro(1) == 1
check_lru(coro, hits=0, misses=1, cache=1, tasks=0, maxsize=1)
assert await coro(2) == 2
check_lru(coro, hits=0, misses=2, cache=1, tasks=0, maxsize=1)
await asyncio.sleep(0)
assert await coro(2) == 2
check_lru(coro, hits=1, misses=2, cache=1, tasks=0, maxsize=1)
assert await coro(1) == 1
check_lru(coro, hits=1, misses=3, cache=1, tasks=0, maxsize=1)
async def test_ttl_with_explicit_invalidation(check_lru: Callable[..., None]) -> None:
@alru_cache(maxsize=None, ttl=0.2)
async def coro(val: int) -> int:
return val
assert await coro(1) == 1
check_lru(coro, hits=0, misses=1, cache=1, tasks=0, maxsize=None)
coro.cache_invalidate(1)
check_lru(coro, hits=0, misses=1, cache=0, tasks=0, maxsize=None)
await asyncio.sleep(0.1)
assert await coro(1) == 1
check_lru(coro, hits=0, misses=2, cache=1, tasks=0, maxsize=None)
await asyncio.sleep(0.1)
# cache is not cleared after ttl expires because invalidate also should clear
# the invalidation by timeout
check_lru(coro, hits=0, misses=2, cache=1, tasks=0, maxsize=None)
async def test_ttl_concurrent() -> None:
@alru_cache(maxsize=1, ttl=1)
async def coro(val: int) -> int:
return val
results = await asyncio.gather(*(coro(i) for i in range(2)))
assert results == list(range(2))
|