cache.config_cache
On this page
Every dataclass and enum in this module is documented in narrative form on the Configuration page (with defaults, environment variables, and cross-references to the code that reads each field). This page is the terse structural reference; see Configuration for guidance and caveats.
Enums
StorageBackend
str, Enum — REDIS = "redis", SQLITE = "sqlite", MEMORY = "memory". Selects the
backend built by build_storage().
EvictionPolicy
str, Enum — REWARD_LRU = "reward_lru", LRU = "lru", LFU = "lfu", TTL = "ttl".
Field type of EvictionConfig.policy. In the analyzed source, CacheManager's eviction
cycle always applies the reward/usage/recency composite scoring from
CachePolicyLearner regardless of which EvictionPolicy is
configured — the enum's other members are not currently branched on anywhere.
EmbeddingProvider
str, Enum — OPENAI = "openai", HUGGINGFACE = "huggingface", OLLAMA = "ollama",
MOCK = "mock". Selects the provider built by
build_embedding_provider().
RoutingDecision
str, Enum — EXACT_HIT = "exact_hit", SEMANTIC_HIT = "semantic_hit",
TENANT_HIT = "tenant_hit", LLM_FALLBACK = "llm_fallback". Returned inside
CacheLookupResult.decision and
RoutingResult.decision.
[!NOTE]
TENANT_HITis declared but never assignedRoutingDecision.TENANT_HITexists as an enum member, andCacheMetricsCollectortracks a_tenant_hitscounter and exposes atenant_hit_ratein its snapshot — but nowhere inCacheManagerisRoutingDecision.TENANT_HITactually returned, nor is_tenant_hitsever incremented, in the analyzed source.tenant_hit_ratewill always read0.0. See Known Issues.
Sub-configuration dataclasses
CacheConfig
@dataclass
class CacheConfig:
storage_backend: StorageBackend = StorageBackend.SQLITE
redis: RedisConfig
sqlite: SQLiteConfig
embedding: EmbeddingConfig
semantic: SemanticConfig
cost: CostConfig
rl: RLConfig
tenant: TenantConfig
security: SecurityConfig
eviction: EvictionConfig
performance: PerformanceConfig
l1_max_items: int = 100
l2_max_items: int = 1000
l3_max_items: int = 10000
l1_max_size_mb: float = 10.0
l2_max_size_mb: float = 100.0
l3_max_size_mb: float = 1000.0
default_ttl: Optional[float] = None
default_ttl_s: Optional[float] = 3600.0
cache_dir: str = "./cache_storage"
log_level: str = "INFO"
enable_stats: bool = True
auto_cleanup_interval: int = 3600
l1_speed_ns: int = 100
l2_speed_ns: int = 1000
l3_speed_ns: int = 10000
l2_to_l1_hits: int = 10
size_bytes: int = 1024
(Field list reflects the class's effective defaults after Python resolves the source's duplicate assignments — see the warning in Configuration.)
Methods
__post_init__(self) -> None
Calls self._validate().
_validate(self) -> None
Raises ValueError for any of:
l1_max_items < 1l2_max_items < l1_max_itemsl3_max_items < l2_max_items- any of
l1_max_size_mb,l2_max_size_mb,l3_max_size_mb<= 0 default_ttl is not None and default_ttl <= 0
from_env(cls) -> CacheConfig (classmethod)
Builds a CacheConfig overriding l1_max_items, l2_max_items, l3_max_items,
cache_dir, and default_ttl from CACHE_L1_MAX_ITEMS, CACHE_L2_MAX_ITEMS,
CACHE_L3_MAX_ITEMS, CACHE_DIR, CACHE_DEFAULT_TTL respectively. Every other field
(including storage_backend, redis, embedding, etc.) takes its normal dataclass
default.
config = CacheConfig.from_env()
to_dict(self) -> dict
Returns a plain dict of exactly eight fields:
l1_max_items, l2_max_items, l3_max_items, l1_max_size_mb, l2_max_size_mb,
l3_max_size_mb, default_ttl, cache_dir. This is a partial serialization — it does
not include storage_backend, redis, embedding, security, or any other
sub-configuration.
Examples
from zerda.cache import CacheConfig, StorageBackend
# Defaults
cfg = CacheConfig()
# From environment
cfg = CacheConfig.from_env()
# Explicit overrides
cfg = CacheConfig(storage_backend=StorageBackend.MEMORY, l1_max_items=200)
# Invalid configuration raises immediately
try:
CacheConfig(l1_max_items=0)
except ValueError as e:
print(e) # "l1_max_items must be at least 1"
Related APIs
- Configuration — full field-by-field guide with defaults and caveats
build_storage()— readsstorage_backend,sqlite,redisCacheManager— the primary consumer ofCacheConfigMultiLevelCache— consumes thel1/l2/l3_max_items,l1/l2/l3_max_size_mb,cache_dir,default_ttl,l2_to_l1_hits, andauto_cleanup_intervalfields