From a16affab6aeda66fa9bda3e928002395df38e034 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Tue, 30 Oct 2018 14:25:18 -0500 Subject: [PATCH 1/3] only cache for 60s on empty search result --- api/search/server.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api/search/server.py b/api/search/server.py index 09b84dbdf8..a45be3ac5e 100644 --- a/api/search/server.py +++ b/api/search/server.py @@ -33,7 +33,6 @@ from api.config import DEFAULT_HOST, DEFAULT_PORT, DEBUG, DEFAULT_CACHE_TIMEOUT from api.config import SEARCH_DEFAULT_LIMIT as DEFAULT_LIMIT -from api.utils import cache_control from .substring_search import search_people_by_name, search_people_by_twitter from .substring_search import search_people_by_username, search_people_by_bio @@ -101,7 +100,6 @@ def test_alphanumeric(query): @searcher.route('/search', methods = ["GET", "POST"], strict_slashes = False) @crossdomain(origin='*') -@cache_control(DEFAULT_CACHE_TIMEOUT) def search_by_name(): query = request.args.get('query') @@ -159,7 +157,13 @@ def search_by_name(): results = {} results['results'] = results_people[:new_limit] - return jsonify(results) + resp = make_response(jsonify(results)) + if len(results['results']) > 0: + cache_timeout = DEFAULT_CACHE_TIMEOUT) + else: + cache_timeout = 60 + + resp.headers['Cache-Control'] = 'public, max-age={:d}'.format(cache_timeout) def search_proofs_index(query): From a4bef8e86a44952c6e3b4e58975eed6fb9d83a85 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Tue, 30 Oct 2018 14:28:55 -0500 Subject: [PATCH 2/3] use env var EMPTY_CACHE_TIMEOUT --- api/config.py | 2 ++ api/search/server.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/api/config.py b/api/config.py index 4ffa7b196f..1657a6f8ce 100644 --- a/api/config.py +++ b/api/config.py @@ -41,6 +41,8 @@ def str2bool(s): # DEFAULT_CACHE_TIMEOUT determines the DEFAULT_CACHE_TIMEOUT = int(os.getenv('DEFAULT_CACHE_TIMEOUT','43200')) # 12 hours in seconds +EMPTY_CACHE_TIMEOUT = int(os.getenv('EMPTY_CACHE_TIMEOUT','60')) # 1 minute + # DEBUG increases logging verbosity DEBUG = str2bool(os.getenv('DEBUG','False')) diff --git a/api/search/server.py b/api/search/server.py index a45be3ac5e..162acdf5cf 100644 --- a/api/search/server.py +++ b/api/search/server.py @@ -31,7 +31,7 @@ from flask import request, jsonify, make_response, render_template, Blueprint from flask_crossdomain import crossdomain -from api.config import DEFAULT_HOST, DEFAULT_PORT, DEBUG, DEFAULT_CACHE_TIMEOUT +from api.config import DEFAULT_HOST, DEFAULT_PORT, DEBUG, DEFAULT_CACHE_TIMEOUT, EMPTY_CACHE_TIMEOUT from api.config import SEARCH_DEFAULT_LIMIT as DEFAULT_LIMIT from .substring_search import search_people_by_name, search_people_by_twitter @@ -159,9 +159,9 @@ def search_by_name(): resp = make_response(jsonify(results)) if len(results['results']) > 0: - cache_timeout = DEFAULT_CACHE_TIMEOUT) + cache_timeout = DEFAULT_CACHE_TIMEOUT else: - cache_timeout = 60 + cache_timeout = EMPTY_CACHE_TIMEOUT resp.headers['Cache-Control'] = 'public, max-age={:d}'.format(cache_timeout) From 7ec3ec9e372de2349a27461d1a260f3a044a6947 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Tue, 30 Oct 2018 14:35:35 -0500 Subject: [PATCH 3/3] remember to _return_ --- api/search/server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/api/search/server.py b/api/search/server.py index 162acdf5cf..4b88d487aa 100644 --- a/api/search/server.py +++ b/api/search/server.py @@ -165,6 +165,7 @@ def search_by_name(): resp.headers['Cache-Control'] = 'public, max-age={:d}'.format(cache_timeout) + return resp def search_proofs_index(query):