Skip to content

Commit 9d17e10

Browse files
committed
Enhance with_solr_cache function to track and handle error results; clear cache entry for transient failures to prevent caching of erroneous data.
1 parent 3d02d81 commit 9d17e10

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/vfbquery/solr_result_cache.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,21 +666,34 @@ def wrapper(*args, **kwargs):
666666
# Cache the result asynchronously to avoid blocking
667667
# Handle DataFrame, dict, and other result types properly
668668
result_is_valid = False
669+
result_is_error = False # Track if result is an error that should clear cache
670+
669671
if result is not None:
670672
if hasattr(result, 'empty'): # DataFrame
671673
result_is_valid = not result.empty
672674
elif isinstance(result, dict):
673675
# For dict results, check if it's not an error result (count != -1)
674676
# Error results should not be cached
675677
if 'count' in result:
676-
result_is_valid = result.get('count', -1) >= 0 # Don't cache errors (count=-1)
678+
count_value = result.get('count', -1)
679+
result_is_valid = count_value >= 0 # Don't cache errors (count=-1)
680+
result_is_error = count_value < 0 # Mark as error if count is negative
677681
else:
678682
result_is_valid = bool(result) # For dicts without count field
679683
elif isinstance(result, (list, str)):
680684
result_is_valid = len(result) > 0
681685
else:
682686
result_is_valid = True
683687

688+
# If result is an error, actively clear any existing cache entry
689+
# This ensures that transient failures don't get stuck in cache
690+
if result_is_error:
691+
logger.warning(f"Query returned error result for {query_type}({term_id}), clearing cache entry")
692+
try:
693+
cache.clear_cache_entry(query_type, cache_term_id)
694+
except Exception as e:
695+
logger.debug(f"Failed to clear cache entry: {e}")
696+
684697
if result_is_valid:
685698
# Validate result before caching for term_info
686699
if query_type == 'term_info':

0 commit comments

Comments
 (0)