Skip to content

Commit 301eec7

Browse files
committed
chore: 优化缓存的配置
1 parent 563bdd8 commit 301eec7

File tree

9 files changed

+45
-87
lines changed

9 files changed

+45
-87
lines changed

core/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import anyio
99
import fastapi
1010

11-
from core import units
12-
from core.abc import ResponseFileLocal, ResponseFileMemory, ResponseFileNotFound, ResponseFileRemote
13-
11+
from tianxiu2b2t import units
12+
from .abc import ResponseFileLocal, ResponseFileMemory, ResponseFileNotFound, ResponseFileRemote
1413
from .locale import load_languages
1514
from .cluster import ClusterManager
1615
from .config import API_VERSION, VERSION, cfg
@@ -210,7 +209,6 @@ def access_log(request: fastapi.Request, response: fastapi.Response, total_time:
210209
def access_log(request: fastapi.Request, response: fastapi.Response, total_time: int):
211210
...
212211

213-
214212
@web.app.middleware("http")
215213
async def auth_middleware(request: fastapi.Request, call_next):
216214
start_time = runtime.get_perf_counter_ns()

core/cluster.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
import anyio
1212
import anyio.abc
1313
import pyzstd as zstd
14-
import tqdm
1514
import socketio
1615

17-
from core import units
16+
from tianxiu2b2t import units
1817

1918
from . import utils
2019
from .abc import BMCLAPIFile, Certificate, CertificateType, OpenBMCLAPIConfiguration, ResponseFile, SocketEmitResult

core/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def concurrency_enable_cluster(self) -> bool:
8484

8585

8686
API_VERSION = "1.13.1"
87-
VERSION = "4.0.5"
87+
VERSION = "4.0.6"
8888
PROJECT = "PythonOpenBMCLAPI"
8989
USER_AGENT = f"openbmclapi-cluster/{API_VERSION} {PROJECT}/{VERSION}"
9090
ROOT_PATH = Path(__file__).parent.parent

core/storage/alist.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import urllib.parse as urlparse
55
import aiohttp
66
import anyio.abc
7-
import cachetools
7+
from tianxiu2b2t import units
88

9-
from core import utils
109
from . import abc
1110
from ..config import USER_AGENT
12-
from ..logger import logger
11+
from .. import utils
1312

1413
class AlistResponse:
1514
def __init__(
@@ -39,7 +38,10 @@ def __init__(
3938
self._endpoint = endpoint
4039
self._username = username
4140
self._password = password
42-
self._redirect_urls: cachetools.TTLCache[str, abc.ResponseFile] = cachetools.TTLCache(maxsize=10000, ttl=60)
41+
self._redirect_urls: utils.UnboundTTLCache[str, abc.ResponseFile] = utils.UnboundTTLCache(
42+
maxsize=int(units.parse_number_units(kwargs.get("cache_size", "10000"))),
43+
ttl=units.parse_time_units(kwargs.get("cache_ttl", "5m"))
44+
)
4345
self._token = None
4446

4547
async def _get_token(self):

core/storage/s3.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import inspect
21
from io import BytesIO
32
import tempfile
43
import time
5-
from typing import Any
64
import aioboto3.session
75
import anyio.abc
86
import anyio.to_thread
97
import aioboto3
108
import urllib.parse as urlparse
9+
from tianxiu2b2t import units
1110

12-
from core import logger
11+
from ..logger import logger
12+
from ..utils import UnboundTTLCache
1313
from . import abc
14-
import cachetools
1514

1615
class S3ResponseMetadata:
1716
def __init__(
@@ -63,8 +62,14 @@ def __init__(
6362
self.custom_s3_host = kwargs.get("custom_s3_host", "")
6463
self.public_endpoint = kwargs.get("public_endpoint", "")
6564
self.session = aioboto3.Session()
66-
self._cache: cachetools.TTLCache[str, abc.ResponseFile] = cachetools.TTLCache(maxsize=10000, ttl=60)
67-
self._cache_files: cachetools.TTLCache[str, abc.FileInfo] = cachetools.TTLCache(maxsize=10000, ttl=60)
65+
self._cache: UnboundTTLCache[str, abc.ResponseFile] = UnboundTTLCache(
66+
maxsize=int(units.parse_number_units(kwargs.get("cache_size", "10000"))),
67+
ttl=units.parse_time_units(kwargs.get("cache_ttl", "5m"))
68+
)
69+
self._cache_files: UnboundTTLCache[str, abc.FileInfo] = UnboundTTLCache(
70+
maxsize=int(units.parse_number_units(kwargs.get("cache_size", "10000"))),
71+
ttl=units.parse_time_units(kwargs.get("cache_files_ttl", "120s"))
72+
)
6873
self._config = {
6974
"endpoint_url": self.endpoint,
7075
"aws_access_key_id": self.access_key,

core/storage/webdav.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import aiowebdav.client
66
import anyio
77
from anyio.abc._tasks import TaskGroup as TaskGroup
8-
import cachetools
8+
from tianxiu2b2t import units
99

10-
from core import logger, utils
11-
from core.config import USER_AGENT
1210
from . import abc
11+
from ..logger import logger
12+
from ..config import USER_AGENT
13+
from .. import utils
14+
1315
import aiowebdav
1416

1517
class WebDavStorage(abc.Storage):
@@ -22,13 +24,20 @@ def __init__(
2224
endpoint: str,
2325
username: str,
2426
password: str,
27+
**kwargs
2528
):
2629
super().__init__(name, path, weight)
2730
self.endpoint = endpoint
2831
self.username = username
2932
self.password = password
30-
self._cache_files: cachetools.TTLCache[str, abc.FileInfo] = cachetools.TTLCache(maxsize=1000, ttl=60)
31-
self._cache_redirects: cachetools.TTLCache[str, abc.ResponseFile] = cachetools.TTLCache(maxsize=1000, ttl=60)
33+
self._cache_redirects: utils.UnboundTTLCache[str, abc.ResponseFile] = utils.UnboundTTLCache(
34+
maxsize=int(units.parse_number_units(kwargs.get("cache_size", "10000"))),
35+
ttl=units.parse_time_units(kwargs.get("cache_ttl", "5m"))
36+
)
37+
self._cache_files: utils.UnboundTTLCache[str, abc.FileInfo] = utils.UnboundTTLCache(
38+
maxsize=int(units.parse_number_units(kwargs.get("cache_size", "10000"))),
39+
ttl=units.parse_time_units(kwargs.get("cache_files_ttl", "120s"))
40+
)
3241
self._mkdir_lock = utils.Lock()
3342

3443
self.client = aiowebdav.client.Client({

core/units.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

core/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from collections import defaultdict, deque
22
import hashlib
33
import io
4+
import math
45
from pathlib import Path
56
import time
67
from typing import Any, Awaitable, Callable, Optional, Coroutine, MutableMapping, TypeVar
78

89
import anyio
910
import anyio.abc
11+
import cachetools
1012
from tqdm import tqdm
1113
from functools import lru_cache
1214

@@ -442,5 +444,13 @@ def __init__(
442444
self.start = start
443445
self.end = end
444446

447+
class UnboundTTLCache(cachetools.TTLCache[K, V]):
448+
def __init__(self, maxsize: Optional[int], ttl: float, timer=time.monotonic):
449+
cachetools.TTLCache.__init__(self, maxsize or math.inf, ttl, timer)
450+
451+
@property
452+
def maxsize(self):
453+
return None
454+
445455
runtime = Runtime()
446456
event = Event()

requirements.txt

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)