diff --git a/app/services/contact_service.py b/app/services/contact_service.py index bc7fdad..e28c883 100644 --- a/app/services/contact_service.py +++ b/app/services/contact_service.py @@ -158,6 +158,16 @@ async def list_contacts( """ 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( Contact.tenant_id == tenant_id, Contact.deleted_at.is_(None), @@ -218,7 +228,7 @@ async def list_contacts( if use_keyset and len(contacts) == page_size and contacts: next_cursor = str(contacts[-1].id) - return { + result = { "items": [_serialize_contact(c) for c in contacts], "total": total, "page": page, @@ -226,6 +236,13 @@ async def list_contacts( "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, user_id: uuid.UUID | None = None, is_system_admin: bool = False) -> dict: