性能: 共享httpx连接池/设置缓存/复合索引自动补建/GZip/分页 ; 商业化v1: APIKey(sha256+scope+限流)/用量账本/版本化端点
This commit is contained in:
+13
-2
@@ -36,11 +36,18 @@ class SettingRepo:
|
||||
"allow_byok": "true",
|
||||
"admin_data_visible": "true", # 默认明文;关闭后加密落库
|
||||
}
|
||||
_cache: dict | None = None # 进程内缓存(读多写少),写入即失效
|
||||
|
||||
@staticmethod
|
||||
async def get(db: AsyncSession, key: str) -> str:
|
||||
v = (await db.execute(select(E.SiteSetting).where(E.SiteSetting.key == key))).scalar_one_or_none()
|
||||
return v.value if v else SettingRepo.DEFAULTS.get(key, "")
|
||||
cache = SettingRepo._cache
|
||||
if cache is not None:
|
||||
return cache.get(key, SettingRepo.DEFAULTS.get(key, ""))
|
||||
rows = (await db.execute(select(E.SiteSetting))).scalars().all()
|
||||
cache = dict(SettingRepo.DEFAULTS)
|
||||
cache.update({r.key: r.value for r in rows})
|
||||
SettingRepo._cache = cache
|
||||
return cache.get(key, "")
|
||||
|
||||
@staticmethod
|
||||
async def set(db: AsyncSession, key: str, value: str):
|
||||
@@ -50,12 +57,16 @@ class SettingRepo:
|
||||
else:
|
||||
db.add(E.SiteSetting(key=key, value=value))
|
||||
await db.commit()
|
||||
SettingRepo._cache = None
|
||||
|
||||
@staticmethod
|
||||
async def all(db: AsyncSession) -> dict:
|
||||
if SettingRepo._cache is not None:
|
||||
return SettingRepo._cache
|
||||
rows = (await db.execute(select(E.SiteSetting))).scalars().all()
|
||||
out = dict(SettingRepo.DEFAULTS)
|
||||
out.update({r.key: r.value for r in rows})
|
||||
SettingRepo._cache = out
|
||||
return out
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user