CacheService
What it is
- A cache service that:
- Reads/writes cached values via an injected
ICacheAdapter. - Serializes values as TEXT, JSON, BINARY, or PICKLE.
- Provides a decorator (
__call__) to cache function results with optional TTL and optional forced refresh.
- Reads/writes cached values via an injected
Public API
Classes
CacheService(adapter: ICacheAdapter)- Main service. Requires an
ICacheAdapterimplementation.
- Main service. Requires an
Methods
-
__call__(key_builder: Callable, cache_type: DataType, ttl: datetime.timedelta | None = None, auto_cache: bool = True)- Returns a decorator that caches the decorated function’s result.
- Cache key is computed by
key_builder(**filtered_args)wherefiltered_argsis a subset of the decorated function’s arguments matchingkey_builder’s parameters. - Cache refresh:
- If the decorated function is called with
force_cache_refresh=<anything>, cache lookup is bypassed and the function is executed; the result is re-cached ifauto_cache=True.
- If the decorated function is called with
- TTL enforcement:
- If
ttlis provided and cache entry is older thancreated_at + ttl,CacheExpiredErroris treated as a miss (function executed).
- If
-
get(key: str, ttl: datetime.timedelta | None = None) -> Any- Fetches and deserializes a cached value by key.
- Applies TTL check if provided.
-
set_text(key: str, value: str) -> None- Stores a string value as
DataType.TEXT. Assertsvalueisstr.
- Stores a string value as
-
set_json(key: str, value: Any) -> None- Stores JSON-serialized value as
DataType.JSON(usesjson.dumps).
- Stores JSON-serialized value as
-
set_binary(key: str, value: bytes) -> None- Stores base64-encoded bytes as
DataType.BINARY. Assertsvalueisbytes.
- Stores base64-encoded bytes as
-
set_pickle(key: str, value: Any) -> None- Stores pickled+base64-encoded value as
DataType.PICKLE.
- Stores pickled+base64-encoded value as
-
exists(key: str) -> bool- Returns whether the key exists in the adapter.
Configuration/Dependencies
- Depends on:
ICacheAdapterfor persistence (get,set,exists).CachedDatamodel returned by adapterget, expected to include:data(serialized string)data_type(DataType)created_at(ISO format string used bydatetime.fromisoformat)
- Uses exceptions from
naas_abi_core.services.cache.CachePort:CacheNotFoundError,CacheExpiredError
Usage
import datetime
from naas_abi_core.services.cache.CacheService import CacheService
from naas_abi_core.services.cache.CachePort import DataType, ICacheAdapter
# You must provide an ICacheAdapter implementation.
adapter: ICacheAdapter = ...
cache = CacheService(adapter)
@cache(lambda user_id: f"user:{user_id}", cache_type=DataType.JSON, ttl=datetime.timedelta(minutes=10))
def load_user(user_id: int):
return {"id": user_id}
# First call computes and caches
u1 = load_user(123)
# Second call returns cached value (if not expired)
u2 = load_user(123)
# Force refresh (bypasses cache lookup)
u3 = load_user(123, force_cache_refresh=True)
Caveats
cache_typemust match the stored entry’sdata_type; if it differs, it is treated as a cache miss and the function is executed (and may be re-cached).- TTL is enforced using
cached_data.created_atparsed viadatetime.fromisoformat(...); adapter must provide an ISO-formatted timestamp string. set_textandset_binaryuseasserttype checks (can be disabled with Python optimization flags).