mirror of
https://github.com/gooba42/adventureland.git
synced 2026-10-24 05:31:26 -05:00
cache data.js (#153)
* cache data.js * fix etag check * use an md5 hash of the data for the etag
This commit is contained in:
@@ -5,6 +5,7 @@ from admin import *
|
|||||||
from crons import *
|
from crons import *
|
||||||
from tests import *
|
from tests import *
|
||||||
from tasks import *
|
from tasks import *
|
||||||
|
import hashlib
|
||||||
|
|
||||||
@ndb.toplevel
|
@ndb.toplevel
|
||||||
@app.route('/comm')
|
@app.route('/comm')
|
||||||
@@ -157,17 +158,37 @@ def serve_codejs():
|
|||||||
else: request.response.set_data("game_log('load_code: Code not found',colors.code_error)")
|
else: request.response.set_data("game_log('load_code: Code not found',colors.code_error)")
|
||||||
return request.response
|
return request.response
|
||||||
|
|
||||||
|
# Cache when data is prepared
|
||||||
|
cached_data = None
|
||||||
|
cached_data_hash = None
|
||||||
|
|
||||||
@app.route('/data.js',methods=['GET','POST'])
|
@app.route('/data.js',methods=['GET','POST'])
|
||||||
@ndb.toplevel
|
@ndb.toplevel
|
||||||
def serve_datajs():
|
def serve_datajs():
|
||||||
domain=gdi(request); additional=""
|
global cached_data
|
||||||
|
global cached_data_hash
|
||||||
|
|
||||||
|
init_request(request)
|
||||||
|
|
||||||
|
additional=""
|
||||||
|
|
||||||
|
if request.values.get("reload"):
|
||||||
|
additional="add_log('Game data reloaded','#32A3B0');\napply_backup()\n"
|
||||||
|
elif cached_data_hash != None and request.headers.get('If-None-Match') == cached_data_hash:
|
||||||
|
# The ETag matches
|
||||||
|
request.response.status_code = 304
|
||||||
|
return request.response
|
||||||
|
|
||||||
|
if cached_data == None or cached_data_hash == None:
|
||||||
geometry={}
|
geometry={}
|
||||||
|
|
||||||
for name in maps:
|
for name in maps:
|
||||||
key=maps[name]["key"]
|
key=maps[name]["key"]
|
||||||
if maps[name].get("ignore"): continue
|
if maps[name].get("ignore"): continue
|
||||||
#if name=="test" and not is_sdk: key="test"
|
#if name=="test" and not is_sdk: key="test"
|
||||||
#logging.info(key)
|
#logging.info(key)
|
||||||
geometry[name]=get_by_iid("map|%s"%key).info.data
|
geometry[name]=get_by_iid("map|%s"%key).info.data
|
||||||
|
|
||||||
data={
|
data={
|
||||||
"version":game_version,
|
"version":game_version,
|
||||||
"achievements":achievements,
|
"achievements":achievements,
|
||||||
@@ -202,8 +223,15 @@ def serve_datajs():
|
|||||||
"docs":docs,
|
"docs":docs,
|
||||||
"drops":drops,
|
"drops":drops,
|
||||||
}
|
}
|
||||||
if request.values.get("reload"): additional="add_log('Game data reloaded','#32A3B0');\napply_backup()\n"
|
|
||||||
request.response.set_data("var G=%s;\n%s"%(json.dumps(data),additional))
|
cached_data = json.dumps(data)
|
||||||
|
cached_data_hash = hashlib.md5(cached_data).hexdigest()
|
||||||
|
|
||||||
|
# The browser should cache this
|
||||||
|
request.response.headers['ETag'] = cached_data_hash
|
||||||
|
request.response.headers['Cache-Control'] = "max-age=31536000" # 1 Year
|
||||||
|
|
||||||
|
request.response.set_data("var G=%s;\n%s"%(cached_data,additional))
|
||||||
return request.response
|
return request.response
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user