Spaces:
Sleeping
Sleeping
Download backend/cache.py from ShivaliGupta/ShopPilot_AI: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/ShivaliGupta/ShopPilot_AI/resolve/main/backend/cache.py
- Command line
-
hf download hf://spaces/ShivaliGupta/ShopPilot_AI/backend/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/ShivaliGupta/ShopPilot_AI/resolve/main/backend/cache.py
1.07 kB
| """ | |
| Tiny in-memory TTL cache. Nothing fancy -- this exists purely so that | |
| repeated searches for the same query during development/demo don't | |
| burn through the free SerpAPI monthly quota (100 searches/month). | |
| Swap this out for Redis if you ever deploy at scale; for a personal | |
| project / hackathon demo this is more than enough. | |
| """ | |
| import time | |
| from typing import Any, Optional | |
| from backend.config import CACHE_TTL_SECONDS | |
| class TTLCache: | |
| def __init__(self, ttl_seconds: int = CACHE_TTL_SECONDS): | |
| self.ttl = ttl_seconds | |
| self._store: dict[str, tuple[float, Any]] = {} | |
| def get(self, key: str) -> Optional[Any]: | |
| entry = self._store.get(key) | |
| if not entry: | |
| return None | |
| expires_at, value = entry | |
| if time.time() > expires_at: | |
| self._store.pop(key, None) | |
| return None | |
| return value | |
| def set(self, key: str, value: Any) -> None: | |
| self._store[key] = (time.time() + self.ttl, value) | |
| def clear(self) -> None: | |
| self._store.clear() | |
| search_cache = TTLCache() | |