Coverage for fss\common\cache\cache.py: 65%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-11 19:09 +0800

1"""Abstract base class for Cache""" 

2 

3from abc import ABC, abstractmethod 

4 

5 

6from fss.common.config import configs 

7 

8 

9class Cache(ABC): 

10 @abstractmethod 

11 async def get(self, key): 

12 """Retrieve a value by key from the cache.""" 

13 pass 

14 

15 @abstractmethod 

16 async def set(self, key, value, timeout=None): 

17 """Set the value of a key in the cache with an optional timeout.""" 

18 pass 

19 

20 @abstractmethod 

21 async def delete(self, key): 

22 """Delete a key from the cache.""" 

23 pass 

24 

25 @abstractmethod 

26 async def exists(self, key): 

27 """Check if a key exists in the cache.""" 

28 pass 

29 

30 

31async def get_cache_client() -> Cache: 

32 """ 

33 Init redis client or page cache client 

34 :return: 

35 """ 

36 if configs.enable_redis: 

37 from fss.common.cache.redis_cache import RedisCache 

38 from fss.common.cache.redis_cache import RedisManager 

39 

40 redis_client = await RedisManager.get_instance() 

41 return RedisCache(redis_client) 

42 from fss.common.cache.page_cache import PageCache 

43 

44 return PageCache()