CachePort
What it is
- A small “port” module defining cache domain types and interfaces:
- Exceptions for cache misses/expiry
- A
CachedDataPydantic model describing stored entries - Interfaces (
ICacheAdapter,ICacheService) to be implemented by concrete cache adapters/services
Public API
Exceptions
CacheNotFoundError- Raised by implementations when a key is missing.
CacheExpiredError- Raised by implementations when an entry is expired (typically when TTL is enforced).
Enums
DataType (Enum[str])- Supported data type labels for cached values:
TEXT,JSON,BINARY,PICKLE
- Supported data type labels for cached values:
Models
CachedData (pydantic.BaseModel)- Represents a cached entry.
- Fields:
key: strdata: Anydata_type: DataTypecreated_at: str(ISO timestamp; defaults todatetime.datetime.now().isoformat())
Interfaces
-
ICacheAdapter- Low-level storage contract.
- Methods (all raise
NotImplementedErrorhere):get(key: str) -> CachedDataset(key: str, value: CachedData) -> Nonedelete(key: str) -> Noneexists(key: str) -> bool
-
ICacheService- Higher-level cache service contract built on an adapter.
- Attributes:
adapter: ICacheAdapter
- Constructor:
__init__(adapter: ICacheAdapter)stores the adapter.
- Methods (all raise
NotImplementedErrorhere):exists(key: str) -> boolget(key: str, ttl: datetime.timedelta | None = None) -> Anyset_text(key: str, value: str) -> Noneset_json(key: str, value: dict) -> Noneset_binary(key: str, value: bytes) -> Noneset_pickle(key: str, value: Any) -> None
Configuration/Dependencies
- Dependencies:
pydantic(BaseModel,Field)- Standard library:
datetime,enum,typing.Any
- No runtime configuration is defined in this module.
Usage
This file defines interfaces; you must implement them to use caching.
import datetime
from naas_abi_core.services.cache.CachePort import (
ICacheAdapter, ICacheService, CachedData, DataType, CacheNotFoundError
)
class InMemoryAdapter(ICacheAdapter):
def __init__(self):
self._store = {}
def get(self, key: str) -> CachedData:
if key not in self._store:
raise CacheNotFoundError(key)
return self._store[key]
def set(self, key: str, value: CachedData) -> None:
self._store[key] = value
def delete(self, key: str) -> None:
self._store.pop(key, None)
def exists(self, key: str) -> bool:
return key in self._store
class SimpleCacheService(ICacheService):
def exists(self, key: str) -> bool:
return self.adapter.exists(key)
def get(self, key: str, ttl: datetime.timedelta | None = None):
return self.adapter.get(key).data
def set_text(self, key: str, value: str) -> None:
self.adapter.set(key, CachedData(key=key, data=value, data_type=DataType.TEXT))
adapter = InMemoryAdapter()
cache = SimpleCacheService(adapter)
cache.set_text("greeting", "hello")
print(cache.get("greeting"))
Caveats
ICacheAdapterandICacheServiceare notabc.ABC; they only raiseNotImplementedError. Implementations must provide behavior.- TTL/expiry semantics are not implemented here;
ttlis only part of theICacheService.getinterface. CachedData.created_atis stored as a string timestamp (ISO format), not adatetimeobject.