cache.storage.factury_fun

On this page

build_storage()

Python
def build_storage(config: CacheConfig) -> BaseStorage

Overview

A match/case factory that constructs the correct BaseStorage implementation based on config.storage_backend. This is how CacheManager obtains its _storage attribute — there is no other call site for this function in the analyzed source, and no public constructor parameter lets a caller inject a pre-built storage instance directly.

Signature

ParameterTypeRequiredNotes
configCacheConfigYesOnly config.storage_backend, config.sqlite, and config.redis are read

Returns

config.storage_backendReturned instance
StorageBackend.MEMORYMemoryStorage()
StorageBackend.SQLITESQLiteStorage(config.sqlite.db_path, wal=config.sqlite.wal_mode, cache_size_kb=config.sqlite.cache_size_kb)
StorageBackend.REDISRedisStorage(config.redis)
anything elseraises ValueError

Raises

ExceptionWhen
ValueErrorconfig.storage_backend doesn't match any of the three known StorageBackend members (defensive case _: branch — in practice unreachable unless storage_backend is set to something other than a StorageBackend enum member, since StorageBackend itself only defines those three values)

Internal Workflow

A single match config.storage_backend: statement with three case arms plus a wildcard case _: that raises. No conditional logic beyond the match itself; no logging.

Examples

Python · 11 lines
from zerda.cache.config_cache import CacheConfig, StorageBackend
from zerda.cache.storage import build_storage

# Memory
storage = build_storage(CacheConfig(storage_backend=StorageBackend.MEMORY))

# SQLite (also the default)
storage = build_storage(CacheConfig())   # StorageBackend.SQLITE by default

# Redis — falls back to SQLite internally if unreachable; see RedisStorage
storage = build_storage(CacheConfig(storage_backend=StorageBackend.REDIS))

Complexity

O(1) — a single dispatch plus one backend constructor call (whose own cost varies: Memory is instant, SQLite performs disk I/O and DDL execution, Redis performs a network round-trip and possibly falls back to SQLite).

Thread Safety

The function itself holds no state and is safe to call from any thread. The safety of the returned object depends on which backend was built — see each backend's own page.

Related pages