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