Skip to content

Commit efcfd9b

Browse files
committed
Enhance caching mechanism for term_info queries by including preview parameter in cache key
1 parent 49185c8 commit efcfd9b

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/vfbquery/solr_result_cache.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,16 +585,23 @@ def wrapper(*args, **kwargs):
585585
logger.warning(f"No term_id found for caching {query_type}")
586586
return func(*args, **kwargs)
587587

588+
# Include preview parameter in cache key for term_info queries
589+
# This ensures preview=True and preview=False have separate cache entries
590+
cache_term_id = term_id
591+
if query_type == 'term_info':
592+
preview = kwargs.get('preview', True) # Default is True
593+
cache_term_id = f"{term_id}_preview_{preview}"
594+
588595
cache = get_solr_cache()
589596

590597
# Clear cache if force_refresh is True
591598
if force_refresh:
592599
logger.info(f"Force refresh requested for {query_type}({term_id})")
593-
cache.clear_cache_entry(query_type, term_id)
600+
cache.clear_cache_entry(query_type, cache_term_id)
594601

595602
# Try cache first (will be empty if force_refresh was True)
596603
if not force_refresh:
597-
cached_result = cache.get_cached_result(query_type, term_id, **kwargs)
604+
cached_result = cache.get_cached_result(query_type, cache_term_id, **kwargs)
598605
if cached_result is not None:
599606
# Validate that cached result has essential fields for term_info
600607
if query_type == 'term_info':
@@ -653,15 +660,15 @@ def wrapper(*args, **kwargs):
653660
if (result and isinstance(result, dict) and
654661
result.get('Id') and result.get('Name')):
655662
try:
656-
cache.cache_result(query_type, term_id, result, **kwargs)
663+
cache.cache_result(query_type, cache_term_id, result, **kwargs)
657664
logger.debug(f"Cached complete result for {term_id}")
658665
except Exception as e:
659666
logger.debug(f"Failed to cache result: {e}")
660667
else:
661668
logger.warning(f"Not caching incomplete result for {term_id}")
662669
else:
663670
try:
664-
cache.cache_result(query_type, term_id, result, **kwargs)
671+
cache.cache_result(query_type, cache_term_id, result, **kwargs)
665672
except Exception as e:
666673
logger.debug(f"Failed to cache result: {e}")
667674

src/vfbquery/vfb_queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,13 +1072,13 @@ def serialize_solr_output(results):
10721072
return json_string
10731073

10741074
@with_solr_cache('term_info')
1075-
def get_term_info(short_form: str, preview: bool = False):
1075+
def get_term_info(short_form: str, preview: bool = True):
10761076
"""
10771077
Retrieves the term info for the given term short form.
10781078
Results are cached in SOLR for 3 months to improve performance.
10791079
10801080
:param short_form: short form of the term
1081-
:param preview: if False, skips executing query previews (much faster)
1081+
:param preview: if True, executes query previews to populate preview_results (default: True)
10821082
:return: term info
10831083
"""
10841084
parsed_object = None

0 commit comments

Comments
 (0)