[docs]classPersistentCacheManager:""" Manages a persistent cache of Python objects on the file system. The PersistentCacheManager class provides a simple interface for caching and retrieving data, using the pickle_io module for pickling to serialize and deserialize the objects. The cache files are stored in the "data" directory, which is created automatically if it does not exist. """_cache_directory="data"# Directory to store cache files@classmethoddef_ensure_directory_exists(cls)->None:"""This method creates the cache directory if it does not already exist."""os.makedirs(cls._cache_directory,exist_ok=True)@classmethoddef_get_cache_file_path(cls,key:str)->str:""" Returns the file path for a cache file based on the given key. Args: key (str): The key associated with the cache file. Returns: str: The file path for the cache file. """returnos.path.join(cls._cache_directory,f"{key}.cache")
[docs]@classmethoddefclear_all_cache(cls)->None:"""Clears the entire cache by deleting all cache files."""forcache_fileinglob.glob(os.path.join(cls._cache_directory,"*.cache")):os.remove(cache_file)
@classmethoddef__getattr__(cls,key:str)->Any:""" Retrieves the cached value for the given key, or None if the key does not exist. Args: key (str): The key associated with the cached value. Returns: Any: The cached value, or None if the key does not exist. """returnread_pickled_data(cls._get_cache_file_path(key))
[docs]@classmethoddefset(cls,key:str,value:Any)->None:""" Sets the cached value for the given key. Args: key (str): The key associated with the cached value. value (Any): The value to be cached. """write_pickled_data(cls._get_cache_file_path(key),value)