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_SOURCES = ["dis", "4ch", "all"]
MANTICORE_SOURCES_RESTRICTED = ["irc"]
MANTICORE_CACHE = True
MANTICORE_CACHE_TIMEOUT = 60
DRILLDOWN_RESULTS_PER_PAGE = 15
DRILLDOWN_DEFAULT_PARAMS = {

View File

@ -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

View File

@ -18,13 +18,14 @@
{% block panel_content %}
{% include 'partials/notify.html' %}
<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 %}
<span class="icon has-tooltip-bottom" data-tooltip="Cached">
<i class="fa-solid fa-database"></i>
</span>
{% endif %}
fetched {{ table.data|length }} of {{ card }} hits in {{ took }}ms
{% if exemption is not None %}
<span class="icon has-tooltip-bottom" data-tooltip="God mode">
<i class="fa-solid fa-book-bible"></i>