Skip to content

Commit 5b13840

Browse files
committed
feat: 新增缓存配置
1 parent 6a484be commit 5b13840

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

dash-fastapi-frontend/.env.dev

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ APP_PORT = 8088
1919
APP_DEBUG = true
2020
# flask-compress压缩配置
2121
APP_COMPRESS_ALGORITHM = 'br'
22-
APP_COMPRESS_BR_LEVEL = 11
22+
APP_COMPRESS_BR_LEVEL = 11
23+
24+
# -------- 缓存配置 --------
25+
# LRU缓存的限制数量,0表示无限制
26+
LRU_CACHE_MAXSIZE = 10000
27+
# LRU缓存不重新分配的容量
28+
LRU_CACHE_CAPACITY = 10000
29+
# TTL缓存的限制数量,0表示无限制
30+
TTL_CACHE_MAXSIZE = 0
31+
# TTL缓存过期时间(秒)
32+
TTL_CACHE_EXPIRE = 600

dash-fastapi-frontend/.env.prod

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ APP_PORT = 8088
1919
APP_DEBUG = false
2020
# flask-compress压缩配置
2121
APP_COMPRESS_ALGORITHM = 'br'
22-
APP_COMPRESS_BR_LEVEL = 11
22+
APP_COMPRESS_BR_LEVEL = 11
23+
24+
# -------- 缓存配置 --------
25+
# LRU缓存的限制数量,0表示无限制
26+
LRU_CACHE_MAXSIZE = 10000
27+
# LRU缓存不重新分配的容量
28+
LRU_CACHE_CAPACITY = 10000
29+
# TTL缓存的限制数量,0表示无限制
30+
TTL_CACHE_MAXSIZE = 0
31+
# TTL缓存过期时间(秒)
32+
TTL_CACHE_EXPIRE = 600

dash-fastapi-frontend/config/env.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ class AppSettings(BaseSettings):
2323
app_compress_br_level: int = 11
2424

2525

26+
class CacheSettings(BaseSettings):
27+
"""
28+
缓存配置
29+
"""
30+
31+
lru_cache_maxsize: int = 10000
32+
lru_cache_capacity: int = 10000
33+
ttl_cache_maxsize: int = 0
34+
ttl_cache_expire: int = 600
35+
36+
2637
class GetConfig:
2738
"""
2839
获取配置
@@ -39,6 +50,14 @@ def get_app_config(self):
3950
# 实例化应用配置模型
4051
return AppSettings()
4152

53+
@lru_cache()
54+
def get_cache_config(self):
55+
"""
56+
获取缓存配置
57+
"""
58+
# 实例化缓存配置模型
59+
return CacheSettings()
60+
4261
@staticmethod
4362
def parse_cli_args():
4463
"""
@@ -66,6 +85,8 @@ def parse_cli_args():
6685
get_config = GetConfig()
6786
# 应用配置
6887
AppConfig = get_config.get_app_config()
88+
# 缓存配置
89+
CacheConfig = get_config.get_cache_config()
6990

7091

7192
class ApiConfig:

dash-fastapi-frontend/utils/cache_util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from cachebox import LRUCache, TTLCache
22
from flask import session
33
from typing import Any, Dict
4+
from config.env import CacheConfig
45

56

6-
cache_manager = LRUCache(maxsize=10000, iterable=None, capacity=10000)
7-
ttl_manager = TTLCache(maxsize=0, ttl=600)
7+
cache_manager = LRUCache(
8+
maxsize=CacheConfig.lru_cache_maxsize,
9+
iterable=None,
10+
capacity=CacheConfig.lru_cache_capacity,
11+
)
12+
ttl_manager = TTLCache(
13+
maxsize=CacheConfig.ttl_cache_maxsize, ttl=CacheConfig.ttl_cache_expire
14+
)
815

916

1017
class CacheManager:

0 commit comments

Comments
 (0)