Skip to content

Commit

Permalink
Merge pull request #880 from blockstack/hotfix/low-cache-on-empty-result
Browse files Browse the repository at this point in the history
Set low cache on empty result
  • Loading branch information
kantai authored Oct 30, 2018
2 parents 0d16602 + 7ec3ec9 commit 4e4796c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down
13 changes: 9 additions & 4 deletions api/search/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
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 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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -159,8 +157,15 @@ 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 = EMPTY_CACHE_TIMEOUT

resp.headers['Cache-Control'] = 'public, max-age={:d}'.format(cache_timeout)

return resp

def search_proofs_index(query):

Expand Down

0 comments on commit 4e4796c

Please sign in to comment.