11
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.oem_resolve import resolve_oem_id
|
||||
from app.models.desktopConfig import DesktopConfig
|
||||
from app.models.oem import Oem
|
||||
from app.models.user import User
|
||||
from app.schemas.admin_desktop_config import DesktopConfigCreate, DesktopConfigUpdate
|
||||
|
||||
@@ -39,31 +39,6 @@ class OemDesktopConfigError(Exception):
|
||||
# 路由层沿用 AdminDesktopConfigError 名称
|
||||
AdminDesktopConfigError = OemDesktopConfigError
|
||||
|
||||
# user_id -> desktop_configs.oem_id(进程内缓存,OEM 绑定变更时需 invalidate)
|
||||
_oem_id_by_user_id: dict[int, int] = {}
|
||||
|
||||
|
||||
def invalidate_oem_id_cache(user_id: int | None = None) -> None:
|
||||
"""清除 resolve_oem_id 缓存;user_id 为 None 时清空全部。"""
|
||||
if user_id is None:
|
||||
_oem_id_by_user_id.clear()
|
||||
else:
|
||||
_oem_id_by_user_id.pop(user_id, None)
|
||||
|
||||
|
||||
async def resolve_oem_id(db: AsyncSession, oem_user: User) -> int:
|
||||
"""desktop_configs.oem_id 对应 oem 表主键;无记录时回退为用户 id。"""
|
||||
user_id = oem_user.id
|
||||
cached = _oem_id_by_user_id.get(user_id)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
oem_pk = await db.scalar(select(Oem.id).where(Oem.user_id == user_id))
|
||||
resolved = int(oem_pk) if oem_pk is not None else user_id
|
||||
_oem_id_by_user_id[user_id] = resolved
|
||||
return resolved
|
||||
|
||||
|
||||
async def _count_by_oem(db: AsyncSession, oem_id: int) -> int:
|
||||
stmt = select(func.count()).select_from(DesktopConfig).where(DesktopConfig.oem_id == oem_id)
|
||||
return await db.scalar(stmt) or 0
|
||||
|
||||
Reference in New Issue
Block a user