Coverage for fss\common\cache\page_cache.py: 62%
21 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-11 19:09 +0800
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-11 19:09 +0800
1"""Simple in-memory page cache implementation"""
3from typing import Any
5import diskcache
7from fss.common.cache.cache import Cache
10class PageCache(Cache):
11 def __init__(self):
12 self.cache = diskcache.Cache()
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)
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)
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
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