Module

cache

Chirp caching framework.

Three levels of caching:

  1. Per-view:@cache_view(ttl=300)
  2. Site-wide:CacheMiddleware(opt-in)
  3. Template fragment:{% cache "key" ttl %}...{% endcache %}

Usage::

from chirp.cache import get_cache, cache_view

@app.route("/products")
@cache_view(ttl=300)
async def products():
    return await db.fetch(Product, "SELECT * FROM products")

# Low-level
cache = get_cache()
await cache.set("key", b"value", ttl=600)

Functions

get_cache 0 CacheBackend | None
Return the current cache backend. None if no cache configured.
def get_cache() -> CacheBackend | None
Returns
CacheBackend | None
set_cache 1 None
Set the cache backend for the current context.
def set_cache(backend: CacheBackend) -> None
Parameters
Name Type Description
backend CacheBackend
create_backend 2 CacheBackend
Create a cache backend by name.
def create_backend(name: str, **kwargs: Any) -> CacheBackend
Parameters
Name Type Description
name str
**kwargs Any
Returns
CacheBackend
cache_view 2 Callable[[Callable[..., …
Decorator to cache a view's response. Usage:: @app.route("/products") …
def cache_view(ttl: int = 300, key_func: Callable[..., str] | None = None) -> Callable[[Callable[..., Any]], Callable[..., Any]]

Decorator to cache a view's response.

Usage::

@app.route("/products")
@cache_view(ttl=300)
async def products():
    ...
Parameters
Name Type Description
ttl int Default:300
key_func Callable[..., str] | None Default:None
Returns
Callable[[Callable[..., Any]], Callable[..., Any]]