Spaces:
Sleeping
Sleeping
Download rag/cache.py from Harhit07/Docmind-RAG: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/Harhit07/Docmind-RAG/resolve/main/rag/cache.py
- Command line
-
hf download hf://spaces/Harhit07/Docmind-RAG/rag/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/Harhit07/Docmind-RAG/resolve/main/rag/cache.py
1.12 kB
| import time | |
| from collections import OrderedDict | |
| from rag.config import settings | |
| class QueryCache: | |
| def __init__(self, maxsize: int = 128, ttl: int = 300): | |
| self._maxsize = maxsize | |
| self._ttl = ttl | |
| self._cache: OrderedDict[str, tuple[float, dict]] = OrderedDict() | |
| def get(self, key: str) -> dict | None: | |
| if key not in self._cache: | |
| return None | |
| ts, value = self._cache[key] | |
| if time.monotonic() - ts > self._ttl: | |
| del self._cache[key] | |
| return None | |
| self._cache.move_to_end(key) | |
| return value | |
| def set(self, key: str, value: dict) -> None: | |
| if key in self._cache: | |
| self._cache.move_to_end(key) | |
| self._cache[key] = (time.monotonic(), value) | |
| if len(self._cache) > self._maxsize: | |
| self._cache.popitem(last=False) | |
| def clear(self) -> None: | |
| self._cache.clear() | |
| def size(self) -> int: | |
| return len(self._cache) | |
| query_cache = QueryCache(maxsize=settings.CACHE_MAXSIZE, ttl=settings.CACHE_TTL_SECONDS) | |