[docs]classCacheConfig:"""Configuration class for managing cache settings and instances. This avoids the use of global variables. """def__init__(self)->None:self._cache:(AsyncMemoryCache|SyncMemoryCache|AsyncRedisCache|SyncRedisCache|None)=SyncMemoryCache()self._unsafe_cache:bool=False
[docs]defcreate_cache(self,cache_type:CacheType,host:str="localhost",port:int=6379,db:int=0,)->AsyncMemoryCache|SyncMemoryCache|AsyncRedisCache|SyncRedisCache:"""Create a cache instance based on the type. Args: cache_type (CacheType): The type of cache to create. host (str): Redis host. Defaults to "localhost". port (int): Redis port. Defaults to 6379. db (int): Redis database number. Defaults to 0. Returns: AsyncMemoryCache | SyncMemoryCache | AsyncRedisCache | SyncRedisCache: The created cache instance. """ifcache_type=="async-redis":returnAsyncRedisCache(host=host,port=port,db=db)ifcache_type=="sync-redis":returnSyncRedisCache(host=host,port=port,db=db)ifcache_type=="async-memory":returnAsyncMemoryCache()ifcache_type=="sync-memory":returnSyncMemoryCache()returnNone
[docs]defset_cache(self,cache_type:CacheType,host:str="localhost",port:int=6379,db:int=0,)->None:"""Set the cache instance based on the configured cache type. Args: cache_type (CacheType): The type of cache to set. host (str): Redis host. Defaults to "localhost". port (int): Redis port. Defaults to 6379. db (int): Redis database number. Defaults to 0. """self._cache=self.create_cache(cache_type,host,port,db)
[docs]defget_cache(self,)->AsyncMemoryCache|SyncMemoryCache|AsyncRedisCache|SyncRedisCache|None:"""Get the cache instance. Returns ------- AsyncMemoryCache | SyncMemoryCache | AsyncRedisCache | SyncRedisCache | None: The current cache instance. """returnself._cache
[docs]defis_unsafe_cache_enabled(self)->bool:"""Check if unsafe cache mode is enabled. Returns ------- bool: True if unsafe cache mode is enabled, otherwise False. """returnself._unsafe_cache
# Singleton instance of CacheConfigcache_config=CacheConfig()