From 9d5e60326acda6622218392f46c6d7f055ddb5b7 Mon Sep 17 00:00:00 2001 From: amy wieliczka Date: Tue, 14 May 2024 17:02:07 -0700 Subject: [PATCH] Add caching to the es_cache_retry module --- exhibits/es_cache_retry.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/exhibits/es_cache_retry.py b/exhibits/es_cache_retry.py index 8302bfc1..6855f5fa 100644 --- a/exhibits/es_cache_retry.py +++ b/exhibits/es_cache_retry.py @@ -14,6 +14,8 @@ from elasticsearch.exceptions import ConnectionError as ESConnectionError from elasticsearch.exceptions import RequestError as ESRequestError +from exhibits.utils import kwargs_md5 + urllib3.disable_warnings() if hasattr(settings, 'XRAY_RECORDER'): @@ -31,6 +33,11 @@ def es_search(body) -> ESResults: + cache_key = f"es_search_{kwargs_md5(**body)}" + cached_results = cache.get(cache_key) + if cached_results: + return cached_results + try: results = elastic_client.search( index=settings.ES_ALIAS, body=json.dumps(body)) @@ -83,6 +90,8 @@ def es_search(body) -> ESResults: results['hits']['total']['value'], facet_counts) + cache.set(cache_key, results, settings.DJANGO_CACHE_TIMEOUT) # seconds + return results def get_thumbnail_key(metadata): @@ -101,8 +110,9 @@ def get_media_key(metadata): key_parts = uri_path.split('/')[2:] return '/'.join(key_parts) -def es_search_nocache(**kwargs): - return es_search(kwargs) + +# def es_search_nocache(**kwargs): +# return es_search(kwargs) def es_get(item_id: str) -> Optional[ESItem]: @@ -110,6 +120,11 @@ def es_get(item_id: str) -> Optional[ESItem]: if not item_id: return None + cache_key = f"es_get_{item_id}" + cached_results = cache.get(cache_key) + if cached_results: + return cached_results + # cannot use Elasticsearch.get() for multi-index alias body = {'query': {'match': {'_id': item_id}}} try: @@ -150,6 +165,7 @@ def es_get(item_id: str) -> Optional[ESItem]: item['children'] = updated_children results = ESItem(found, item, item_search) + cache.set(cache_key, results, settings.DJANGO_CACHE_TIMEOUT) # seconds return results