feat: I.4 Performance — Redis cache for contact list queries (60s TTL, first 3 pages, no search), connection pooling already exists (pool_size=20), selectinload already used, 3 performance test files exist
This commit is contained in:
@@ -158,6 +158,16 @@ async def list_contacts(
|
|||||||
"""
|
"""
|
||||||
from app.core.visibility import apply_visibility_filter
|
from app.core.visibility import apply_visibility_filter
|
||||||
|
|
||||||
|
# I.4 Performance: Cache simple list queries (no search, no cursor, first 3 pages)
|
||||||
|
use_cache = not search and not cursor and page <= 3 and not folder_id
|
||||||
|
cache_key = f"contacts:list:{tenant_id}:{page}:{page_size}:{contact_type or 'all'}:{sort_by}:{sort_order}:{user_id or 'admin'}:{is_system_admin}"
|
||||||
|
if use_cache:
|
||||||
|
from app.core.cache import cache_get
|
||||||
|
cached = await cache_get(cache_key)
|
||||||
|
if cached:
|
||||||
|
return cached
|
||||||
|
|
||||||
|
|
||||||
base = select(Contact).where(
|
base = select(Contact).where(
|
||||||
Contact.tenant_id == tenant_id,
|
Contact.tenant_id == tenant_id,
|
||||||
Contact.deleted_at.is_(None),
|
Contact.deleted_at.is_(None),
|
||||||
@@ -218,7 +228,7 @@ async def list_contacts(
|
|||||||
if use_keyset and len(contacts) == page_size and contacts:
|
if use_keyset and len(contacts) == page_size and contacts:
|
||||||
next_cursor = str(contacts[-1].id)
|
next_cursor = str(contacts[-1].id)
|
||||||
|
|
||||||
return {
|
result = {
|
||||||
"items": [_serialize_contact(c) for c in contacts],
|
"items": [_serialize_contact(c) for c in contacts],
|
||||||
"total": total,
|
"total": total,
|
||||||
"page": page,
|
"page": page,
|
||||||
@@ -226,6 +236,13 @@ async def list_contacts(
|
|||||||
"next_cursor": next_cursor,
|
"next_cursor": next_cursor,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# I.4 Performance: Cache the result for simple queries
|
||||||
|
if use_cache:
|
||||||
|
from app.core.cache import cache_set
|
||||||
|
await cache_set(cache_key, result, ttl=60) # 60 second cache
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
async def get_contact(db: AsyncSession, tenant_id: uuid.UUID, contact_id: str,
|
async def get_contact(db: AsyncSession, tenant_id: uuid.UUID, contact_id: str,
|
||||||
user_id: uuid.UUID | None = None, is_system_admin: bool = False) -> dict:
|
user_id: uuid.UUID | None = None, is_system_admin: bool = False) -> dict:
|
||||||
|
|||||||
Reference in New Issue
Block a user