-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_cache.py
More file actions
55 lines (42 loc) · 1.41 KB
/
async_cache.py
File metadata and controls
55 lines (42 loc) · 1.41 KB
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
# /// script
# requires-python = ">=3.10"
# dependencies = ["warp_cache"]
# ///
"""Async caching — warp_cache auto-detects async functions."""
import asyncio
import logging
from warp_cache import cache
logging.basicConfig(level=logging.INFO, format="%(message)s")
log = logging.getLogger(__name__)
@cache(max_size=64, ttl=10.0)
async def fetch_user(user_id):
"""Simulate an async API call."""
log.info(" [miss] fetching user %s", user_id)
await asyncio.sleep(0.1) # simulate network latency
return {"id": user_id, "name": f"User {user_id}"}
async def main():
# First call: cache miss
user = await fetch_user(42)
log.info("Got: %s", user)
# Second call: instant cache hit (no network round-trip)
user = await fetch_user(42)
log.info("Got: %s (cached)", user)
# Concurrent fetches for different keys — all miss
users = await asyncio.gather(
fetch_user(1),
fetch_user(2),
fetch_user(3),
)
log.info("Fetched %d users concurrently", len(users))
# Re-fetch the same users — all hit from cache
log.info("\nRe-fetching same users (all cached)...")
users = await asyncio.gather(
fetch_user(1),
fetch_user(2),
fetch_user(3),
fetch_user(42),
)
log.info("Fetched %d users from cache", len(users))
log.info("\nCache info: %s", fetch_user.cache_info())
if __name__ == "__main__":
asyncio.run(main())