mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
refactor: 拆分职责、引入 dataclass 封装并增强缓存健壮性
- ErrorClassifier 副作用操作分离为 ErrorHandlerService(缓存失效、健康记录、RPM 调整) - chat_handler_base 提取 ProviderRequestResult dataclass 和 _prepare_provider_request 方法 - failover 提取 AttemptErrorOutcome dataclass 和辅助方法 - formula_engine 拆分 _resolve_mapping 为子方法,增加求值异常日志 - usage recording 引入 UsageCostInfo dataclass 封装成本参数 - 前端 types.ts 拆分为 types/ 子模块 - cache backend 工厂函数加锁防止并发重复创建,LocalCache 容量检查修正 - CacheSync 监听增加断线重连机制,publish 增加重试 - guide 页面修正 useSiteInfo() 调用顺序
This commit is contained in:
15
src/plugins/cache/memory.py
vendored
15
src/plugins/cache/memory.py
vendored
@@ -6,7 +6,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
from typing import Any
|
||||
@@ -24,7 +23,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
super().__init__(name, config)
|
||||
self._cache: OrderedDict = OrderedDict()
|
||||
self._expiry: dict[str, float] = {}
|
||||
self._lock = threading.RLock()
|
||||
self._lock = asyncio.Lock()
|
||||
self._hits = 0
|
||||
self._misses = 0
|
||||
self._evictions = 0
|
||||
@@ -60,7 +59,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
now = time.time()
|
||||
expired_keys = []
|
||||
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
for key, expiry in self._expiry.items():
|
||||
if expiry < now:
|
||||
expired_keys.append(key)
|
||||
@@ -81,7 +80,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
|
||||
async def get(self, key: str) -> Any | None:
|
||||
"""获取缓存值"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
# 检查是否过期
|
||||
if key in self._expiry:
|
||||
if self._expiry[key] < time.time():
|
||||
@@ -103,7 +102,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
|
||||
async def set(self, key: str, value: Any, ttl: int | None = None) -> bool:
|
||||
"""设置缓存值"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
# 检查大小限制
|
||||
if key not in self._cache:
|
||||
self._check_size()
|
||||
@@ -126,7 +125,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
|
||||
async def delete(self, key: str) -> bool:
|
||||
"""删除缓存项"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
if key in self._cache:
|
||||
self._cache.pop(key)
|
||||
self._expiry.pop(key, None)
|
||||
@@ -135,7 +134,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
|
||||
async def exists(self, key: str) -> bool:
|
||||
"""检查缓存项是否存在"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
# 检查是否过期
|
||||
if key in self._expiry:
|
||||
if self._expiry[key] < time.time():
|
||||
@@ -147,7 +146,7 @@ class MemoryCachePlugin(CachePlugin):
|
||||
|
||||
async def clear(self) -> bool:
|
||||
"""清空所有缓存"""
|
||||
with self._lock:
|
||||
async with self._lock:
|
||||
self._cache.clear()
|
||||
self._expiry.clear()
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user