Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CACHE]feat: change time to live logic in cache #431

Merged
merged 8 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 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 Down Expand Up @@ -106,29 +106,28 @@ def get_es_search_response():
cached_search_results = cache_strategy(
cache_key,
get_es_search_response,
self.should_cache_search_response,
get_cache_time_to_live(self.search_params),
self.should_cache_for_how_long,
)

self.total_results = cached_search_results["total_results"]
self.es_search_results = cached_search_results["response"]
self.execution_time = cached_search_results["execution_time"]

def should_cache_search_response(self):
"""Cache search response if execution time is higher than 400 ms
or if the query terms are a siren or a siret."""
def should_cache_for_how_long(self):
"""Determines how long to cache search results based on conditions:
- 24 hours if execution time > MIN_EXECUTION_TIME
- 30 minutes if searching by SIREN/SIRET
- No caching (0 minutes) otherwise or on error"""
try:
query_terms = self.search_params.terms
if (
self.execution_time > MIN_EXECUTION_TIME
or is_siren(query_terms)
or is_siret(query_terms)
):
return True
return False
if self.execution_time > MIN_EXECUTION_TIME:
return timedelta(hours=24)
if is_siren(query_terms) or is_siret(query_terms):
return timedelta(minutes=30)
return timedelta(minutes=0) # Default case when no conditions are met
except KeyError as error:
logging.info(f"Error getting search execution time: {error}")
return False
return timedelta(minutes=0)

def run(self):
if self.search_type == SearchType.TEXT:
Expand Down
27 changes: 0 additions & 27 deletions app/elastic/helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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 @@ -84,27 +81,3 @@ 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 for a shorter period
if is_siren(query_terms) or is_siret(query_terms):
return timedelta(minutes=30)

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

except (AttributeError, KeyError):
return timedelta(minutes=15)
9 changes: 3 additions & 6 deletions app/service/convention_collective.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
from app.utils.cache import cache_strategy
from app.utils.helpers import fetch_json_from_url

TIME_TO_LIVE = timedelta(days=1)


def should_cache_search_response():
return True
def should_cache_for_how_long():
return timedelta(hours=24)


def get_metadata_json():
Expand All @@ -29,8 +27,7 @@ def get_metadata_cc_response():
json_content = cache_strategy(
cache_key,
get_metadata_json,
should_cache_search_response,
TIME_TO_LIVE,
should_cache_for_how_long,
)
return JSONResponse(json_content)

Expand Down
9 changes: 3 additions & 6 deletions app/service/last_modified.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from app.utils.cache import cache_strategy
from app.utils.helpers import fetch_json_from_url

TIME_TO_LIVE = timedelta(days=1)


def should_cache_search_response():
return True
def should_cache_for_how_long():
return timedelta(hours=24)


def get_updates_json():
Expand All @@ -22,7 +20,6 @@ def get_last_modified_response():
json_content = cache_strategy(
cache_key,
get_updates_json,
should_cache_search_response,
TIME_TO_LIVE,
should_cache_for_how_long,
)
return JSONResponse(json_content)
8 changes: 4 additions & 4 deletions app/utils/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
from collections.abc import Callable
from datetime import timedelta

from app.utils.helpers import hash_string
from app.utils.redis import RedisClient
Expand Down Expand Up @@ -28,8 +29,7 @@ def set_cache_value(cache_client, key, value, time_to_live):
def cache_strategy(
key,
get_value: Callable,
should_cache_value: Callable,
time_to_live,
should_cache_for_how_long: Callable,
):
try:
redis_client_cache = RedisClient()
Expand All @@ -40,8 +40,8 @@ def cache_strategy(
if cached_value:
return json.loads(cached_value)
value_to_cache = get_value()
should_cache = should_cache_value()
if should_cache:
time_to_live = should_cache_for_how_long()
if time_to_live > timedelta(minutes=0):
set_cache_value(
redis_client_cache,
request_cache_key,
Expand Down
Loading