caching-patternslisted
Install: claude install-skill dbtinoy-/lexigram-framework-skills
# Caching Patterns
## Overview
Multi-backend caching behind `CacheBackendProtocol` — swap in-memory, Redis, Memcached, or custom backends via config without changing service code.
## Basic Usage
```python
from lexigram.contracts.cache import CacheBackendProtocol
class ProductService:
def __init__(self, cache: CacheBackendProtocol):
self.cache = cache
async def get_price(self, product_id: str) -> Result[float, DomainError]:
cached = await self.cache.get(f"price:{product_id}")
if cached.is_ok():
return Ok(float(cached.unwrap()))
price = await self._compute_price(product_id)
await self.cache.set(f"price:{product_id}", str(price), ttl=300)
return Ok(price)
```
## Named Backends
```python
from typing import Annotated
from lexigram.di import Named
class ReportingService:
def __init__(
self,
fast_cache: Annotated[CacheBackendProtocol, Named("session")],
slow_cache: Annotated[CacheBackendProtocol, Named("analytics")],
): ...
```
## Stampede Protection
```python
# Built into Redis backend — deduplicates concurrent misses
# Only one caller recomputes; others wait for the result
await self.cache.get_or_compute(
key="expensive:data",
factory=compute_expensive_data,
ttl=300,
stale_ttl=3600, # serve stale while re-fetching
)
```
## Key Prefixing
```yaml
# Backend handles prefix automatically from config
cache:
backend: redis
redis:
url: redis://localhos