Coverage for fss\common\cache\page_cache.py: 62%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-12 22:20 +0800

1"""Simple in-memory page cache implementation""" 

2 

3from typing import Any 

4 

5import diskcache 

6 

7from fss.common.cache.cache import Cache 

8 

9 

10class PageCache(Cache): 

11 def __init__(self): 

12 self.cache = diskcache.Cache() 

13 

14 async def get(self, key: str) -> Any: 

15 """Retrieve a value by key from the in-memory cache.""" 

16 return self.cache.get(key) 

17 

18 async def set(self, key: str, value: Any, timeout: int = None) -> None: 

19 """Set the value for a key in the in-memory cache.""" 

20 self.cache.set(key, value) 

21 if timeout: 

22 self.cache.expire(key, timeout) 

23 

24 async def delete(self, key: str) -> bool: 

25 """Delete a key from the in-memory cache.""" 

26 if key in self.cache: 

27 self.cache.delete(key) 

28 return True 

29 return False 

30 

31 async def exists(self, key: str) -> bool: 

32 """Check if a key exists in the in-memory cache.""" 

33 if key in self.cache: 

34 return True 

35 return False