Spaces:
Running
Running
Download app/cache_utils.py from TGAshwinYT/EventHorizon-Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/TGAshwinYT/EventHorizon-Backend/resolve/main/app/cache_utils.py
- Command line
-
hf download hf://spaces/TGAshwinYT/EventHorizon-Backend/app/cache_utils.py
-
curl -L -o cache_utils.py https://huggingface.co/spaces/TGAshwinYT/EventHorizon-Backend/resolve/main/app/cache_utils.py
1.05 kB
| import time | |
| import asyncio | |
| from typing import Any, Dict | |
| class TTLCache: | |
| """ | |
| A Time-To-Live Hash Map Data Structure. | |
| Provides O(1) time complexity for insertions and lookups. | |
| Automatically invalidates entries older than `ttl_seconds`. | |
| """ | |
| def __init__(self, ttl_seconds: int = 3600): | |
| self.ttl = ttl_seconds | |
| self.cache: Dict[str, Dict[str, Any]] = {} | |
| def get(self, key: str) -> Any: | |
| """O(1) time complexity lookup.""" | |
| if key in self.cache: | |
| entry = self.cache[key] | |
| if time.time() - entry['timestamp'] < self.ttl: | |
| return entry['value'] | |
| else: | |
| # O(1) time complexity deletion | |
| del self.cache[key] | |
| return None | |
| def set(self, key: str, value: Any): | |
| """O(1) time complexity insertion.""" | |
| self.cache[key] = { | |
| 'timestamp': time.time(), | |
| 'value': value | |
| } | |
| def clear(self): | |
| """O(1) operation to reset the entire cache.""" | |
| self.cache.clear() | |