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,27 +68,33 @@ def construct_query(query, size, index, blank=False):
def run_query(client, user, search_query): def run_query(client, user, search_query):
start = time.process_time() if settings.MANTICORE_CACHE:
query_normalised = json.dumps(search_query, sort_keys=True) start = time.process_time()
hash = siphash(hash_key, query_normalised) query_normalised = json.dumps(search_query, sort_keys=True)
cache_hit = r.get(f"query_cache.{user.id}.{hash}") hash = siphash(hash_key, query_normalised)
if cache_hit: cache_hit = r.get(f"query_cache.{user.id}.{hash}")
print("Cache hit") if cache_hit:
response = json.loads(cache_hit) print("Cache hit")
time_took = (time.process_time() - start) * 1000 response = json.loads(cache_hit)
# Round to 3 significant figures time_took = (time.process_time() - start) * 1000
time_took_rounded = round(time_took, 3 - int(floor(log10(abs(time_took)))) - 1) # Round to 3 significant figures
response["took"] = time_took_rounded time_took_rounded = round(
response["cache"] = True time_took, 3 - int(floor(log10(abs(time_took)))) - 1
return response )
response["took"] = time_took_rounded
response["cache"] = True
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)
print("Writing to cache")
to_write_cache = json.dumps(response) # Write cache
r.set(f"query_cache.{user.id}.{hash}", to_write_cache) if settings.MANTICORE_CACHE:
r.expire(f"query_cache.{user.id}.{hash}", 30) print("Writing to cache")
print("Written 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 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>