Skip to content

Commit

Permalink
Add caching to the es_cache_retry module
Browse files Browse the repository at this point in the history
  • Loading branch information
amywieliczka committed May 15, 2024
1 parent 2580949 commit 9d5e603
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions exhibits/es_cache_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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))
Expand Down Expand Up @@ -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):
Expand All @@ -101,15 +110,21 @@ 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]:
# cannot search Elasticsearch with empty string
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:
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 9d5e603

Please sign in to comment.