CacheFSAdapter
What it is
- A filesystem-backed cache adapter implementing
ICacheAdapter. - Stores each cache entry as a JSON file under a configured directory.
- Uses SHA-256 of the cache key as the filename.
Public API
class CacheFSAdapter(ICacheAdapter)__init__(cache_dir: str)- Ensures
cache_direxists (creates it if missing).
- Ensures
get(key: str) -> CachedData- Loads and returns cached data from the corresponding JSON file.
- Raises
CacheNotFoundErrorif the cache file does not exist.
set(key: str, value: CachedData) -> None- Writes
valueas JSON to the file derived fromkey.
- Writes
delete(key: str) -> None- Deletes the cache file derived from
key. - Raises
CacheNotFoundErrorif the cache file does not exist.
- Deletes the cache file derived from
exists(key: str) -> bool- Returns
Trueif the cache file forkeyexists, elseFalse.
- Returns
Configuration/Dependencies
- Inputs:
cache_dir: directory path used to store cache files.
- Depends on:
naas_abi_core.services.cache.CachePort:CachedData(data model; must supportCachedData(**dict)andmodel_dump()),CacheNotFoundError,ICacheAdapter.
Usage
from naas_abi_core.services.cache.adapters.secondary.CacheFSAdapter import CacheFSAdapter
from naas_abi_core.services.cache.CachePort import CachedData, CacheNotFoundError
cache = CacheFSAdapter(cache_dir=".cache")
key = "my-cache-key"
value = CachedData() # populate fields as required by your CachedData model
cache.set(key, value)
if cache.exists(key):
loaded = cache.get(key)
print(loaded)
try:
cache.delete(key)
except CacheNotFoundError:
pass
Caveats
- Cache filenames are SHA-256 hashes of keys; original keys are not used as filenames.
get()/delete()raiseCacheNotFoundErrorwhen the corresponding file is missing.- Stored format is JSON generated from
CachedData.model_dump(); loading expects JSON to matchCachedData(**data).