davidr70 commited on
Commit
e519933
·
1 Parent(s): ac1fffc

add caching

Browse files
Files changed (2) hide show
  1. data_access.py +4 -0
  2. requirements.txt +2 -1
data_access.py CHANGED
@@ -5,6 +5,7 @@ from typing import Optional
5
 
6
  import asyncpg
7
  import psycopg2
 
8
  from dotenv import load_dotenv
9
  import pandas as pd
10
 
@@ -36,6 +37,7 @@ async def get_questions(conn: asyncpg.Connection):
36
  questions = await conn.fetch("SELECT id, question_text FROM questions ORDER BY id")
37
  return [{"id": q["id"], "text": q["question_text"]} for q in questions]
38
 
 
39
  async def get_metadata(conn: asyncpg.Connection, question_id: int, source_finder_id_run_id: int):
40
  metadata = await conn.fetchrow('''
41
  SELECT metadata
@@ -54,6 +56,7 @@ async def get_source_finders(conn: asyncpg.Connection):
54
 
55
 
56
  # Get distinct run IDs for a question
 
57
  async def get_run_ids(conn: asyncpg.Connection, source_finder_id: int, question_id: int = None):
58
  query = """
59
  select distinct sfr.description, srs.source_finder_run_id as run_id
@@ -243,6 +246,7 @@ async def get_unified_sources(conn: asyncpg.Connection, question_id: int, source
243
  return unified_results, stats_df
244
 
245
 
 
246
  async def get_source_text(conn: asyncpg.Connection, tractate_chunk_id: int):
247
  """
248
  Retrieves the text content for a given tractate chunk ID.
 
5
 
6
  import asyncpg
7
  import psycopg2
8
+ from cachetools import TTLCache, cached
9
  from dotenv import load_dotenv
10
  import pandas as pd
11
 
 
37
  questions = await conn.fetch("SELECT id, question_text FROM questions ORDER BY id")
38
  return [{"id": q["id"], "text": q["question_text"]} for q in questions]
39
 
40
+ @cached(cache=TTLCache(ttl=1800, maxsize=1024))
41
  async def get_metadata(conn: asyncpg.Connection, question_id: int, source_finder_id_run_id: int):
42
  metadata = await conn.fetchrow('''
43
  SELECT metadata
 
56
 
57
 
58
  # Get distinct run IDs for a question
59
+ @cached(cache=TTLCache(ttl=1800, maxsize=1024))
60
  async def get_run_ids(conn: asyncpg.Connection, source_finder_id: int, question_id: int = None):
61
  query = """
62
  select distinct sfr.description, srs.source_finder_run_id as run_id
 
246
  return unified_results, stats_df
247
 
248
 
249
+ @cached(cache=TTLCache(ttl=1800, maxsize=1024))
250
  async def get_source_text(conn: asyncpg.Connection, tractate_chunk_id: int):
251
  """
252
  Retrieves the text content for a given tractate chunk ID.
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  asyncpg
2
  gradio
3
  dotenv
4
- psycopg2
 
 
1
  asyncpg
2
  gradio
3
  dotenv
4
+ psycopg2
5
+ cachetools