From 753c1689407e1819d3ab9874306cdf0ebc845f49 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 5 Sep 2022 07:20:30 +0100 Subject: [PATCH] Make cache configurable --- app/local_settings.example.py | 2 ++ core/lib/manticore.py | 42 +++++++++++++---------- core/templates/widgets/table_results.html | 11 +++--- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/local_settings.example.py b/app/local_settings.example.py index 3508e36..d9b46b6 100644 --- a/app/local_settings.example.py +++ b/app/local_settings.example.py @@ -24,6 +24,8 @@ MANTICORE_MAIN_SIZES = ["20", "50", "100", "200", "400", "800"] MANTICORE_MAIN_SIZES_ANON = ["20", "50", "100"] MANTICORE_MAIN_SOURCES = ["dis", "4ch", "all"] MANTICORE_SOURCES_RESTRICTED = ["irc"] +MANTICORE_CACHE = True +MANTICORE_CACHE_TIMEOUT = 60 DRILLDOWN_RESULTS_PER_PAGE = 15 DRILLDOWN_DEFAULT_PARAMS = { diff --git a/core/lib/manticore.py b/core/lib/manticore.py index 447012a..7e403d1 100644 --- a/core/lib/manticore.py +++ b/core/lib/manticore.py @@ -68,27 +68,33 @@ def construct_query(query, size, index, blank=False): def run_query(client, user, search_query): - start = time.process_time() - query_normalised = json.dumps(search_query, sort_keys=True) - hash = siphash(hash_key, query_normalised) - cache_hit = r.get(f"query_cache.{user.id}.{hash}") - if cache_hit: - print("Cache hit") - response = json.loads(cache_hit) - time_took = (time.process_time() - start) * 1000 - # Round to 3 significant figures - time_took_rounded = round(time_took, 3 - int(floor(log10(abs(time_took)))) - 1) - response["took"] = time_took_rounded - response["cache"] = True - return response + if settings.MANTICORE_CACHE: + start = time.process_time() + query_normalised = json.dumps(search_query, sort_keys=True) + hash = siphash(hash_key, query_normalised) + cache_hit = r.get(f"query_cache.{user.id}.{hash}") + if cache_hit: + print("Cache hit") + response = json.loads(cache_hit) + time_took = (time.process_time() - start) * 1000 + # Round to 3 significant figures + time_took_rounded = round( + time_took, 3 - int(floor(log10(abs(time_took)))) - 1 + ) + response["took"] = time_took_rounded + response["cache"] = True + return response response = client.search(search_query) response = response.to_dict() filter_blacklisted(user, response) - print("Writing to cache") - to_write_cache = json.dumps(response) - r.set(f"query_cache.{user.id}.{hash}", to_write_cache) - r.expire(f"query_cache.{user.id}.{hash}", 30) - print("Written to cache") + + # Write cache + if settings.MANTICORE_CACHE: + print("Writing to cache") + to_write_cache = json.dumps(response) + r.set(f"query_cache.{user.id}.{hash}", to_write_cache) + r.expire(f"query_cache.{user.id}.{hash}", settings.MANTICORE_CACHE_TIMEOUT) + print("Written to cache") return response diff --git a/core/templates/widgets/table_results.html b/core/templates/widgets/table_results.html index 0b7be5a..edc17b0 100644 --- a/core/templates/widgets/table_results.html +++ b/core/templates/widgets/table_results.html @@ -18,13 +18,14 @@ {% block panel_content %} {% include 'partials/notify.html' %} - - - - - showing {{ table.data|length }} of {{ card }} hits in {{ took }}ms {% if cache is not None %} + + + {% endif %} + + fetched {{ table.data|length }} of {{ card }} hits in {{ took }}ms + {% if exemption is not None %}