From a8027a05bb477ea9e140aa3920c1283a7e8b74b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Mon, 24 Feb 2025 16:57:25 +0900 Subject: [PATCH] cache tenant database --- backend/app/core/cache.py | 11 ++++++----- backend/app/core/dbmanager.py | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/app/core/cache.py b/backend/app/core/cache.py index 7843d17..071fe33 100644 --- a/backend/app/core/cache.py +++ b/backend/app/core/cache.py @@ -1,8 +1,8 @@ import time from typing import Any from sqlalchemy.orm import Session -from app.db.cruddb import domainService -from app.db.cruddb import tenantService +from app.db.cruddb import domainService,tenantService +from app.db.session import Database class MemoryCache: def __init__(self, max_cache_size: int = 100, ttl: int = 60): @@ -60,12 +60,13 @@ class tenantCache: def __init__(self): self.memoryCache = MemoryCache(max_cache_size=50, ttl=120) - + def get_tenant_db(self,db: Session, tenantid: str): if not self.memoryCache.get(f"TENANT_{tenantid}"): tenant = tenantService.get_tenant(db,tenantid) if tenant: - self.memoryCache.set(f"TENANT_{tenantid}",tenant.db) - return self.memoryCache.get(f"TENANT_{tenantid}") + database = Database(tenant.db) + self.memoryCache.set(f"TENANT_{tenantid}",database) + return self.memoryCache.get(f"TENANT_{tenantid}") tenantCacheService =tenantCache() \ No newline at end of file diff --git a/backend/app/core/dbmanager.py b/backend/app/core/dbmanager.py index abf62eb..8c0fa4e 100644 --- a/backend/app/core/dbmanager.py +++ b/backend/app/core/dbmanager.py @@ -1,13 +1,13 @@ from fastapi import Depends,Request -from app.db.session import get_tenant_db,get_user_db +from app.db.session import get_tenant_db from app.core import tenantCacheService from app.db.session import tenantdb def get_db(request: Request,tenant:str = "1",tenantdb = Depends(get_tenant_db)): - db_url = tenantCacheService.get_tenant_db(tenantdb,tenant) - db = get_user_db(db_url) + database = tenantCacheService.get_tenant_db(tenantdb,tenant) try: + db = database.get_db() request.state.tenant = tenant request.state.db = db yield db