Tiered Cache¶
Two-level cache architecture: L1 (local LRU) + L2 (Redis), with no additional external dependencies.
Usage¶
from redis_kit import Cache, ConnectionManager
from redis_kit.cache import TieredCache
conn = ConnectionManager(url="redis://localhost:6379/0")
redis_cache = Cache(conn.sync_client, prefix="myapp:cache")
cache = TieredCache(
redis_cache,
local_maxsize=2000, # L1 max entries
local_ttl=30.0, # L1 TTL (seconds)
negative_ttl=5.0, # Cache misses for 5s
)
Read Path¶
- Check L1 (local cache) -- instant response, no network overhead
- If miss, check L2 (Redis)
- If L2 hit, auto-backfill L1
- If both levels miss, write a negative cache entry to L1 (prevents repeated L2 penetration)
Write Path¶
Write-through: set() writes to both L1 and L2.
Batch Operations¶
get_many queries L1 first, only sending missed keys to L2:
Local Cache Management¶
cache.invalidate_local("user:1") # Clear one key from L1
cache.clear_local() # Clear all L1
print(cache.local_size) # Current L1 entry count