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:
@@ -17,6 +17,7 @@ import cProfile
|
|||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
import operator
|
import operator
|
||||||
|
import time
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
@@ -483,7 +484,19 @@ def response():
|
|||||||
else:
|
else:
|
||||||
mimetype = 'application/json'
|
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
|
return response_decorated_function
|
||||||
|
|
||||||
|
@@ -89,7 +89,7 @@ function renderTableAndChart(url, container_id, table_id, chart_id, link_param,
|
|||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: makeURI(url),
|
url: makeURI(url),
|
||||||
dataType: "jsonp",
|
dataType: "json",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
|
|
||||||
var tableData = [];
|
var tableData = [];
|
||||||
@@ -325,7 +325,7 @@ function initSingleSelector(name, api_url, select2_extra_options, change_handler
|
|||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: api_url,
|
url: api_url,
|
||||||
dataType: "jsonp",
|
dataType: "json",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
var initial_value = getUrlVars()[name];
|
var initial_value = getUrlVars()[name];
|
||||||
if (initial_value) {
|
if (initial_value) {
|
||||||
|
@@ -71,11 +71,12 @@ def get_vault():
|
|||||||
|
|
||||||
if not getattr(flask.request, 'stackalytics_updated', None):
|
if not getattr(flask.request, 'stackalytics_updated', None):
|
||||||
time_now = utils.date_to_timestamp('now')
|
time_now = utils.date_to_timestamp('now')
|
||||||
may_update_by_time = (time_now > vault.get('vault_update_time', 0) +
|
may_update_by_time = time_now > vault.get('vault_next_update_time', 0)
|
||||||
cfg.CONF.dashboard_update_interval)
|
|
||||||
if may_update_by_time:
|
if may_update_by_time:
|
||||||
flask.request.stackalytics_updated = True
|
flask.request.stackalytics_updated = True
|
||||||
vault['vault_update_time'] = time_now
|
vault['vault_update_time'] = time_now
|
||||||
|
vault['vault_next_update_time'] = (
|
||||||
|
time_now + cfg.CONF.dashboard_update_interval)
|
||||||
memory_storage_inst = vault['memory_storage']
|
memory_storage_inst = vault['memory_storage']
|
||||||
have_updates = memory_storage_inst.update(compact_records(
|
have_updates = memory_storage_inst.update(compact_records(
|
||||||
vault['runtime_storage'].get_update(os.getpid())))
|
vault['runtime_storage'].get_update(os.getpid())))
|
||||||
|
Reference in New Issue
Block a user