1. 移除多个文件中的空行、冗余导入
2. 修复文件末尾缺少换行符的问题
3. 新增并补全飞书多类工具API实现:
- 多维表格:更新、删除记录,列出视图
- 文档:更新、追加、删除块
- 云文档:上传、下载文件
- 群组:创建、添加成员、更新信息、创建公告
- 目录:重构用户部门缓存逻辑
4. 优化消息发送、回复、转发等API的错误处理和逻辑
5. 新增消息列表查询、已读状态查询等功能
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import time
|
|
from typing import Any
|
|
|
|
DEFAULT_MAX_ENTRIES = 1000
|
|
DEFAULT_TTL_S = 600
|
|
|
|
|
|
class FeishuSentCache:
|
|
def __init__(self, max_entries: int = DEFAULT_MAX_ENTRIES, ttl_s: int = DEFAULT_TTL_S):
|
|
self._cache: dict[str, dict[str, Any]] = {}
|
|
self._timestamps: dict[str, float] = {}
|
|
self._max_entries = max_entries
|
|
self._ttl_s = ttl_s
|
|
self._lock = asyncio.Lock()
|
|
|
|
async def cache_sent(self, msg_id: str, chat_id: str, metadata: dict[str, Any] | None = None) -> None:
|
|
async with self._lock:
|
|
self._evict_expired()
|
|
key = self._make_key(msg_id, chat_id)
|
|
self._cache[key] = {"message_id": msg_id, "chat_id": chat_id, "metadata": metadata or {}}
|
|
self._timestamps[key] = time.monotonic()
|
|
if len(self._cache) > self._max_entries:
|
|
oldest = min(self._timestamps, key=self._timestamps.get)
|
|
self._cache.pop(oldest, None)
|
|
self._timestamps.pop(oldest, None)
|
|
|
|
async def get_sent(self, msg_id: str, chat_id: str) -> dict[str, Any] | None:
|
|
async with self._lock:
|
|
key = self._make_key(msg_id, chat_id)
|
|
entry = self._cache.get(key)
|
|
if entry is None:
|
|
return None
|
|
ts = self._timestamps.get(key, 0)
|
|
if time.monotonic() - ts > self._ttl_s:
|
|
self._cache.pop(key, None)
|
|
self._timestamps.pop(key, None)
|
|
return None
|
|
return entry
|
|
|
|
async def invalidate(self, msg_id: str, chat_id: str) -> None:
|
|
async with self._lock:
|
|
key = self._make_key(msg_id, chat_id)
|
|
self._cache.pop(key, None)
|
|
self._timestamps.pop(key, None)
|
|
|
|
@staticmethod
|
|
def _make_key(msg_id: str, chat_id: str) -> str:
|
|
return f"{chat_id}:{msg_id}"
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.monotonic()
|
|
expired = [k for k, ts in self._timestamps.items() if now - ts > self._ttl_s]
|
|
for k in expired:
|
|
self._cache.pop(k, None)
|
|
self._timestamps.pop(k, None)
|
|
|
|
async def clear(self) -> None:
|
|
async with self._lock:
|
|
self._cache.clear()
|
|
self._timestamps.clear()
|