Allow cliend-side caching

Add Expires and Cache-Control headers to API responses. This allows
browsers to cache them and avoid extra round-trips for data. Also use
plain JSON type in UI to allow browser to use cache. For widget to work
added "Access-Control-Allow-Origin: *" header to all API requests. Note
that althogh this will make cross-origin requests work, it's still not
enough to make browser cache such response, so widget won't benefit from
client-side caching yet.

Change-Id: I49eeffccaf79fe3417bd941590b5dd239ddc433f
This commit is contained in:
Yuriy Taraday
2014-08-19 00:25:22 +04:00
parent 1274bbdbff
commit 03ca988d43
3 changed files with 19 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ import cProfile
import functools
import json
import operator
import time
import flask
from oslo.config import cfg
@@ -483,7 +484,19 @@ def response():
else:
mimetype = 'application/json'
return flask.current_app.response_class(data, mimetype=mimetype)
resp = flask.current_app.response_class(data, mimetype=mimetype)
update_time = vault.get_vault()['vault_next_update_time']
now = utils.date_to_timestamp('now')
if now < update_time:
max_age = update_time - now
else:
max_age = 0
resp.headers['cache-control'] = 'public, max-age=%d' % (max_age,)
resp.headers['expires'] = time.strftime(
'%a, %d %b %Y %H:%M:%S GMT',
time.gmtime(vault.get_vault()['vault_next_update_time']))
resp.headers['access-control-allow-origin'] = '*'
return resp
return response_decorated_function