loom.core.cache.gateway¶
Functions
|
Return |
Classes
|
Facade over aiocache with msgpack serialization for entity data. |
- class loom.core.cache.gateway.CacheGateway(*, alias='default')[source]¶
Bases:
objectFacade over aiocache with msgpack serialization for entity data.
Two distinct usage modes:
Data gateway — configured with
MsgspecSerializer. Used byCachedRepositoryfor entity and list storage. All values are msgpack-encoded on write and decoded on read.Counter gateway — configured without a serializer (raw backend). Used by
GenerationalDependencyResolverfor generation counter storage. Values are stored as plain Python integers, enabling atomic native increment on both Redis andSimpleMemoryCache.The gateway auto-detects which mode it is in at construction time and routes
incr()accordingly — no manual configuration needed beyond choosing the right aiocache alias.- Parameters:
alias (str) – Registered aiocache alias to retrieve the backend from.
Example:
data_gateway = CacheGateway(alias=config.aiocache_alias) counter_gateway = CacheGateway(alias=config.effective_counter_alias) resolver = GenerationalDependencyResolver(counter_gateway)
- static configure(raw_config)[source]¶
Apply a configuration mapping to the global aiocache registry.
- classmethod apply_config(config)[source]¶
Configure aiocache from a
CacheConfig.Equivalent to
configure()but also injectsconfig.max_sizeinto everyaiocache.SimpleMemoryCachebackend entry that does not already declare its ownmax_size. Entries for other backend types (Redis, Memcached, …) are forwarded unchanged.- Parameters:
config (CacheConfig) – Resolved cache configuration.
- Return type:
None
Example:
cache_cfg = CacheConfig( aiocache_alias="cache", counter_alias="counters", max_size=1000, aiocache_config={ "cache": {"cache": "aiocache.SimpleMemoryCache", ...}, "counters": {"cache": "aiocache.SimpleMemoryCache"}, }, ) CacheGateway.apply_config(cache_cfg)
- async get_value(key: str, *, type: type[T]) T | None[source]¶
- async get_value(key: str, *, type: None = None) Any
Retrieve a cached value, optionally converting it to the given type.
- async multi_get_values(keys, *, type=None)[source]¶
Retrieve multiple values in a single round-trip.
- async multi_set_values(pairs, ttl=None)[source]¶
Store multiple key-value pairs in a single round-trip.
- async incr(key, delta=1)[source]¶
Increment a numeric value at the given key.
Routes to the optimal strategy based on the backend configuration detected at construction time:
Raw backend (no serializer): delegates to the backend’s native
incrementmethod when available. On Redis this is an atomicINCR/INCRBYcommand; onSimpleMemoryCacheit is asyncio-safe. Falls back to GET+SET for backends that do not exposeincrement.Serialized backend (
MsgspecSerializer): uses a non-atomic GET+SET. Correct for single-process deployments; the asyncio event loop does not preempt between the two awaits within a single coroutine execution.