Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /src /grid /infrastructure /cache.py
| import asyncio | |
| import logging | |
| import time | |
| from abc import ABC, abstractmethod | |
| from typing import Any | |
| logger = logging.getLogger(__name__) | |
| class CacheInterface(ABC): | |
| """Abstract interface for caching operations.""" | |
| async def get(self, key: str) -> Any | None: | |
| pass | |
| async def set(self, key: str, value: Any, ttl: int = 300) -> None: | |
| pass | |
| async def delete(self, key: str) -> None: | |
| pass | |
| async def clear(self) -> None: | |
| pass | |
| class MemoryCache(CacheInterface): | |
| """ | |
| In-memory cache implementation using Python dict. | |
| Not shared across processes. Suitable for local dev. | |
| """ | |
| def __init__(self): | |
| self._store: dict[str, dict[str, Any]] = {} | |
| self._lock = asyncio.Lock() | |
| async def get(self, key: str) -> Any | None: | |
| async with self._lock: | |
| data = self._store.get(key) | |
| if not data: | |
| return None | |
| if data["expires_at"] < time.time(): | |
| del self._store[key] | |
| return None | |
| return data["value"] | |
| async def set(self, key: str, value: Any, ttl: int = 300) -> None: | |
| async with self._lock: | |
| self._store[key] = {"value": value, "expires_at": time.time() + ttl} | |
| async def delete(self, key: str) -> None: | |
| async with self._lock: | |
| self._store.pop(key, None) | |
| async def clear(self) -> None: | |
| async with self._lock: | |
| self._store.clear() | |
| class CacheFactory: | |
| def create(backend: str = "memory") -> CacheInterface: | |
| if backend == "memory": | |
| logger.info("Initializing MemoryCache") | |
| return MemoryCache() | |
| # Reserved for 'sqlite' or 'redis' implementations later | |
| logger.warning(f"Unknown backend '{backend}', falling back to MemoryCache") | |
| return MemoryCache() | |
Xet Storage Details
- Size:
- 1.95 kB
- Xet hash:
- 20fbe812b39da8d654ebff8cff9f7d0ade149a86414023c08bb7f53836035c15
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.