lythonic.compose.cached¶
Cache utilities for wrapping callables with SQLite-backed caching.
Cached: caching layer for callables via register_cached_callable.
Wraps sync or async methods that return dict or Pydantic BaseModel with
SQLite-backed caching. Each cached method gets its own table with typed
parameter columns (derived from the method signature) and a composite
primary key.
Usage¶
from pathlib import Path
from lythonic.compose.cached import register_cached_callable
from lythonic.compose.namespace import Namespace
ns = Namespace()
register_cached_callable(
ns, "myapp.downloads:fetch_prices",
min_ttl=0.5, max_ttl=2.0, db_path=Path("cache.db"), nsref="market:fetch_prices",
)
# Sync method — served from cache when fresh
result = ns.market.fetch_prices(ticker="AAPL")
# Async method — awaited on cache miss or hard expiry
register_cached_callable(
ns, "myapp.downloads:get_exchange_rate",
min_ttl=0.25, max_ttl=1.0, db_path=Path("cache.db"), nsref="get_exchange_rate",
)
rate = await ns.get_exchange_rate(from_currency="USD", to_currency="EUR")
TTL Behavior¶
- age <
min_ttl: return cached value (fresh) min_ttl<= age <max_ttl: probabilistic refresh — probability increases linearly from 0 to 1. On refresh failure, returns stale value.- age >=
max_ttlor cache miss: call original method. On failure, raises.
Validation¶
All method parameters must have types registered as simple_type in
KNOWN_TYPES (primitives, date, datetime, Path). Validated at registration
time via Method.validate_simple_type_args().
Pushback¶
When a cached method raises CacheRefreshPushback(days, namespace_prefix), all
probabilistic refreshes matching the scope are suppressed for the given duration.
If namespace_prefix is omitted, only the raising method is suppressed.
- During the probabilistic window with active pushback: returns stale data.
- Past
max_ttlwith active pushback: raisesCacheRefreshSuppressed. - Cache miss: always calls the method regardless of pushback.
Namespace¶
Wrapped methods are installed on a Namespace object with nested attribute
access. The nsref uses colon-separated format
(e.g., "market:fetch_prices" becomes ns.market.fetch_prices(...)).
CacheRefreshPushback
¶
Bases: Exception
Raise from a cached method to suppress probabilistic refreshes.
Defaults to suppressing only the raising method; set namespace_prefix
to suppress a group of methods.
Source code in src/lythonic/compose/cached.py
CacheRefreshSuppressed
¶
Bases: Exception
Raised when a cache entry is past max_ttl but refresh is suppressed by an active pushback.
Source code in src/lythonic/compose/cached.py
register_cached_callable(ns, gref, min_ttl, max_ttl, db_path, nsref=None)
¶
Register a callable with cache wrapping. Handles DDL generation,
wrapper building, pushback table creation, and namespace registration.
Stores cache config in node.metadata["cache"].
Source code in src/lythonic/compose/cached.py
generate_cache_table_ddl(table_name, method)
¶
Generate CREATE TABLE DDL for a cache table based on the method's parameter types.