CacheFSAdapter
What it is
- A filesystem-backed cache adapter implementing
ICacheAdapter. - Stores each cache entry as a JSON file in a given directory, named by the SHA-256 hash of the cache key.
- Uses a re-entrant lock (
threading.RLock) to serialize operations within a process.
Public API
class CacheFSAdapter(ICacheAdapter)__init__(cache_dir: str)- Ensures
cache_direxists (creates it if missing).
- Ensures
get(key: str) -> CachedData- Reads the JSON cache file for
keyand returns aCachedData. - Raises
CacheNotFoundErrorif the entry does not exist.
- Reads the JSON cache file for
set(key: str, value: CachedData) -> None- Writes/overwrites the cache entry for
key. - Uses a temporary file and
os.replace()for atomic replacement.
- Writes/overwrites the cache entry for
set_if_absent(key: str, value: CachedData) -> bool- Writes the cache entry only if it does not already exist.
- Returns
Trueif written,Falseif already present.
delete(key: str) -> None- Deletes the cache entry for
key. - Raises
CacheNotFoundErrorif the entry does not exist.
- Deletes the cache entry for
exists(key: str) -> bool- Returns
Trueif the cache entry exists, elseFalse.
- Returns
Configuration/Dependencies
- Requires:
cache_dir: directory where cache files are stored.CachedData,CacheNotFoundError,ICacheAdapterfromnaas_abi_core.services.cache.CachePort.
- Storage format:
- JSON (
value.model_dump()), pretty-printed withindent=4.
- JSON (
- File naming:
sha256(key.encode()).hexdigest().
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-key"
value = CachedData(...) # fill required fields for your CachedData model
cache.set(key, value)
if cache.exists(key):
loaded = cache.get(key)
try:
cache.delete(key)
except CacheNotFoundError:
pass
Caveats
- Concurrency:
- Thread-safe within a single process via
RLock. - No inter-process locking; concurrent writers from multiple processes may race.
- Thread-safe within a single process via
- Key privacy/traceability:
- Filenames are hashes of keys (keys not stored as filenames), but values are stored in plaintext JSON.
- Error behavior:
get()anddelete()raiseCacheNotFoundErrorwhen the entry file is missing.