cache tenant database

This commit is contained in:
2025-02-24 16:57:25 +09:00
parent c0672f2487
commit a8027a05bb
2 changed files with 9 additions and 8 deletions

View File

@@ -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()