Cache backends
CacheBackend protocol, MemoryCache, FileCache, default key.
A cache backend implements three methods: get, set, delete. Bridle ships two concrete backends — memory and file — and reserves Redis for v0.2.0.
CacheBackend
@runtime_checkable
class CacheBackend(Protocol):
def get(self, key: str) -> Any: ...
def set(self, key: str, value: Any, ttl: float | None = None) -> None: ...
def delete(self, key: str) -> None: ...Available from bridle.cache.
| Method | Description |
|---|---|
get(key) | Return the cached value, or bridle.cache.MISS if absent or expired. |
set(key, value, ttl=None) | Store. ttl in seconds; None means no expiry. |
delete(key) | Remove. No-op if absent. |
MISS
from bridle.cache import MISSThe sentinel returned by get when there is no value. Distinct from None, so caching None is distinguishable from a miss.
default_cache_key
bridle.cache.default_cache_key(call: Call) -> strThe deterministic key used when cache(...) is not given an explicit key=. Hashes the call's kind, schema fingerprint, prompt, JSON-encoded context, and tools fingerprint. Returns a "bridle:<32-hex>" string.
Stable across processes for the same logical call.
MemoryCache
from bridle.cache.memory import MemoryCache
MemoryCache()Process-local, dict-backed, threading-safe. TTL honored on read.
| Method | Notes |
|---|---|
get(key) | Returns MISS for missing or expired entries; deletes expired on read. |
set(key, value, ttl=None) | Stores (value, expires_at) under key. |
delete(key) | Removes the key. |
clear() | Removes everything. (Convenience beyond the protocol.) |
The implicit default when no backend is set via bridle.set_cache.
FileCache
from bridle.cache.file import FileCache
FileCache(path: str | pathlib.Path)One pickle file per key under path. Atomic writes via tempfile + os.replace. TTL honored on read.
| Method | Notes |
|---|---|
get(key) | Returns MISS on missing file, unpickle error, or expired entry. |
set(key, value, ttl=None) | Writes atomically via a tempfile in the same directory. |
delete(key) | Removes the file; ignored if absent. |
Keys are sha-derived in default_cache_key, so they are filesystem-safe. Path separators are defensively replaced if you pass a custom key=.
RedisCache
Reserved for v0.2.0. The class exists in bridle.cache.redis but raises on construction in v0.1.0.