性能: 共享httpx连接池/设置缓存/复合索引自动补建/GZip/分页 ; 商业化v1: APIKey(sha256+scope+限流)/用量账本/版本化端点

This commit is contained in:
zhiqui
2026-11-15 01:37:59 +08:00
parent 18ae7e1f9d
commit 495083f9b4
7 changed files with 283 additions and 32 deletions
+13 -2
View File
@@ -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
+6
View File
@@ -49,3 +49,9 @@ async def init_db():
await conn.execute(text(
f"UPDATE {table.name} SET {col.name}=0 "
f"WHERE {col.name}=''"))
# 旧表补建索引(create_all 只对新建表生效)
for table in Base.metadata.tables.values():
for idx in table.indexes:
await conn.execute(text(
f'CREATE INDEX IF NOT EXISTS "{idx.name}" ON "{table.name}" '
f'({", ".join(f"\'{c.name}\'" for c in idx.columns)})'))