获取缓存数据 Args: key: 缓存键 ttl: 存活时间(秒),默认15分钟 Returns: 缓存的值,如果不存在或已过期则返回None
(self, key: str, ttl: int = 900)
| 67 | self._lock = Lock() |
| 68 | |
| 69 | def get(self, key: str, ttl: int = 900) -> Optional[Any]: |
| 70 | """ |
| 71 | 获取缓存数据 |
| 72 | |
| 73 | Args: |
| 74 | key: 缓存键 |
| 75 | ttl: 存活时间(秒),默认15分钟 |
| 76 | |
| 77 | Returns: |
| 78 | 缓存的值,如果不存在或已过期则返回None |
| 79 | """ |
| 80 | with self._lock: |
| 81 | if key in self._cache: |
| 82 | # 检查是否过期 |
| 83 | if time.time() - self._timestamps[key] < ttl: |
| 84 | return self._cache[key] |
| 85 | else: |
| 86 | # 已过期,删除缓存 |
| 87 | del self._cache[key] |
| 88 | del self._timestamps[key] |
| 89 | return None |
| 90 | |
| 91 | def set(self, key: str, value: Any) -> None: |
| 92 | """ |
no outgoing calls