CacheFactory
What it is
- A small factory for creating a
CacheServicebacked by a filesystem cache (CacheFSAdapter). - Automatically locates (or creates) a
storagedirectory and places the cache under a configurable subpath.
Public API
class CacheFactory@staticmethod CacheFS_find_storage(subpath: str = "cache", needle: str = "storage") -> CacheService- Returns a
CacheServiceconfigured with aCacheFSAdapterrooted at:<found_storage_folder>/<subpath>
- Ensures
subpathis prefixed with"cache"(e.g.,"foo"becomes"cache/foo"). - If no storage folder is found, creates
./storagein the current working directory and retries.
- Returns a
Configuration/Dependencies
- Depends on:
naas_abi_core.services.cache.CacheService.CacheServicenaas_abi_core.services.cache.adapters.secondary.CacheFSAdapter.CacheFSAdapternaas_abi_core.utils.Storage.find_storage_foldernaas_abi_core.utils.Storage.NoStorageFolderFound
- Filesystem behavior:
- Uses
os.getcwd()as the starting point for locating the storage folder. - Creates
os.path.join(os.getcwd(), "storage")if a storage folder is not found.
- Uses
Usage
from naas_abi_core.services.cache.CacheFactory import CacheFactory
cache = CacheFactory.CacheFS_find_storage() # uses subpath "cache"
cache2 = CacheFactory.CacheFS_find_storage("myapp") # uses subpath "cache/myapp"
Caveats
- If no storage folder is found, this will create a
storage/directory in the current working directory.