Make cache configurable

This commit is contained in:
Mark Veidemanis 2022-09-05 07:20:30 +01:00
parent 958eb2b549
commit 753c168940
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 32 additions and 23 deletions

View File

@ -24,6 +24,8 @@ MANTICORE_MAIN_SIZES = ["20", "50", "100", "200", "400", "800"]
MANTICORE_MAIN_SIZES_ANON = ["20", "50", "100"] MANTICORE_MAIN_SIZES_ANON = ["20", "50", "100"]
MANTICORE_MAIN_SOURCES = ["dis", "4ch", "all"] MANTICORE_MAIN_SOURCES = ["dis", "4ch", "all"]
MANTICORE_SOURCES_RESTRICTED = ["irc"] MANTICORE_SOURCES_RESTRICTED = ["irc"]
MANTICORE_CACHE = True
MANTICORE_CACHE_TIMEOUT = 60
DRILLDOWN_RESULTS_PER_PAGE = 15 DRILLDOWN_RESULTS_PER_PAGE = 15
DRILLDOWN_DEFAULT_PARAMS = { DRILLDOWN_DEFAULT_PARAMS = {

View File

@ -68,6 +68,7 @@ def construct_query(query, size, index, blank=False):
def run_query(client, user, search_query): def run_query(client, user, search_query):
if settings.MANTICORE_CACHE:
start = time.process_time() start = time.process_time()
query_normalised = json.dumps(search_query, sort_keys=True) query_normalised = json.dumps(search_query, sort_keys=True)
hash = siphash(hash_key, query_normalised) hash = siphash(hash_key, query_normalised)
@ -77,17 +78,22 @@ def run_query(client, user, search_query):
response = json.loads(cache_hit) response = json.loads(cache_hit)
time_took = (time.process_time() - start) * 1000 time_took = (time.process_time() - start) * 1000
# Round to 3 significant figures # Round to 3 significant figures
time_took_rounded = round(time_took, 3 - int(floor(log10(abs(time_took)))) - 1) time_took_rounded = round(
time_took, 3 - int(floor(log10(abs(time_took)))) - 1
)
response["took"] = time_took_rounded response["took"] = time_took_rounded
response["cache"] = True response["cache"] = True
return response return response
response = client.search(search_query) response = client.search(search_query)
response = response.to_dict() response = response.to_dict()
filter_blacklisted(user, response) filter_blacklisted(user, response)
# Write cache
if settings.MANTICORE_CACHE:
print("Writing to cache") print("Writing to cache")
to_write_cache = json.dumps(response) to_write_cache = json.dumps(response)
r.set(f"query_cache.{user.id}.{hash}", to_write_cache) r.set(f"query_cache.{user.id}.{hash}", to_write_cache)
r.expire(f"query_cache.{user.id}.{hash}", 30) r.expire(f"query_cache.{user.id}.{hash}", settings.MANTICORE_CACHE_TIMEOUT)
print("Written to cache") print("Written to cache")
return response return response

View File

@ -18,13 +18,14 @@
{% block panel_content %} {% block panel_content %}
{% include 'partials/notify.html' %} {% include 'partials/notify.html' %}
<script src="{% static 'js/column-shifter.js' %}"></script> <script src="{% static 'js/column-shifter.js' %}"></script>
<span class="icon has-tooltip-bottom">
<i class="fa-solid fa-chart-mixed"></i>
</span>
showing {{ table.data|length }} of {{ card }} hits in {{ took }}ms
{% if cache is not None %} {% if cache is not None %}
<span class="icon has-tooltip-bottom" data-tooltip="Cached">
<i class="fa-solid fa-database"></i>
</span>
{% endif %} {% endif %}
fetched {{ table.data|length }} of {{ card }} hits in {{ took }}ms
{% if exemption is not None %} {% if exemption is not None %}
<span class="icon has-tooltip-bottom" data-tooltip="God mode"> <span class="icon has-tooltip-bottom" data-tooltip="God mode">
<i class="fa-solid fa-book-bible"></i> <i class="fa-solid fa-book-bible"></i>