Skip to content

Commit

Permalink
feat(cache): add different ttl based on search params
Browse files Browse the repository at this point in the history
  • Loading branch information
HAEKADI committed Oct 28, 2024
1 parent 03fd620 commit e27cb8f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/elastic/es_search_runner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from datetime import timedelta

from app.elastic.es_index import StructureMapping
from app.elastic.geo_search import build_es_search_geo_query
from app.elastic.helpers.helpers import (
execute_and_agg_total_results_by_identifiant,
extract_ul_and_etab_from_es_response,
get_cache_time_to_live,
page_through_results,
)
from app.elastic.parsers.siren import is_siren
Expand All @@ -15,7 +15,6 @@
from app.utils.cache import cache_strategy
from app.utils.helpers import is_dev_env

TIME_TO_LIVE = timedelta(days=31)
MIN_EXECUTION_TIME = 400
MAX_TOTAL_RESULTS = 10000

Expand Down Expand Up @@ -108,7 +107,7 @@ def get_es_search_response():
cache_key,
get_es_search_response,
self.should_cache_search_response,
TIME_TO_LIVE,
get_cache_time_to_live(self.search_params),
)

self.total_results = cached_search_results["total_results"]
Expand Down
28 changes: 28 additions & 0 deletions app/elastic/helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import timedelta

from app.elastic.parsers.siren import is_siren
from app.elastic.parsers.siret import is_siret


def extract_ul_and_etab_from_es_response(structure):
Expand Down Expand Up @@ -81,3 +84,28 @@ def compute_doc_id(page_etablissements):

def sort_search_by_company_size(es_search_builder):
return es_search_builder.search_params.sort_by_size is True


def get_cache_time_to_live(search_params):
"""
Determine how long to cache search results based on search parameters.
Args:
search_params: Search parameters object containing query terms
Returns:
timedelta: How long to cache the results
"""
try:
query_terms = search_params.terms

# Cache SIREN/SIRET lookups longer since they're exact matches
# and this data changes less frequently
if is_siren(query_terms) or is_siret(query_terms):
return timedelta(minutes=30)

# For regular text searches, cache for a shorter period
return timedelta(hours=24)

except (AttributeError, KeyError):
return timedelta(minutes=15)

0 comments on commit e27cb8f

Please sign in to comment.