Skip to content

Commit

Permalink
feat(db): Add Supabase client and database instances caching (#2513)
Browse files Browse the repository at this point in the history
This pull request adds caching for the Supabase client and database
instances in order to improve performance and reduce unnecessary API
calls. The `get_supabase_client()` and `get_supabase_db()` functions now
check if the instances have already been created and return the cached
instances if available. This avoids creating new instances for every
function call, resulting in faster execution times.
  • Loading branch information
StanGirard authored Apr 28, 2024
1 parent bdb115a commit 30b9e05
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions backend/models/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from uuid import UUID

from langchain.embeddings.ollama import OllamaEmbeddings
Expand Down Expand Up @@ -120,17 +121,26 @@ class ResendSettings(BaseSettings):
resend_api_key: str = "null"


# Global variables to store the Supabase client and database instances
_supabase_client: Optional[Client] = None
_supabase_db: Optional[SupabaseDB] = None


def get_supabase_client() -> Client:
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
supabase_client: Client = create_client(
settings.supabase_url, settings.supabase_service_key
)
return supabase_client
global _supabase_client
if _supabase_client is None:
settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
_supabase_client = create_client(
settings.supabase_url, settings.supabase_service_key
)
return _supabase_client


def get_supabase_db() -> SupabaseDB:
supabase_client = get_supabase_client()
return SupabaseDB(supabase_client)
global _supabase_db
if _supabase_db is None:
_supabase_db = SupabaseDB(get_supabase_client())
return _supabase_db


def get_embeddings():
Expand Down

0 comments on commit 30b9e05

Please sign in to comment.