Improve ETag caching support for image tiles

This commit is contained in:
Mike Primm 2018-12-07 21:32:28 -06:00
parent b24f92cdd0
commit 505e6b3b90
2 changed files with 30 additions and 8 deletions

View File

@ -28,6 +28,7 @@ public class MapStorageResourceHandler extends AbstractHandler {
private DynmapCore core;
private byte[] blankpng;
private long blankpnghash = 0x12345678;
public MapStorageResourceHandler() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -91,8 +92,23 @@ public class MapStorageResourceHandler extends AbstractHandler {
tr = tile.read();
tile.releaseReadLock();
}
response.setHeader("Cache-Control", "max-age=0,must-revalidate");
String etag;
if (tr == null) {
etag = "\"" + blankpnghash + "\"";
}
else {
etag = "\"" + tr.hashCode + "\"";
}
response.setHeader("ETag", etag);
String ifnullmatch = request.getHeader("If-None-Match");
if ((ifnullmatch != null) && ifnullmatch.equals(etag)) {
response.sendError(HttpStatus.NOT_MODIFIED_304);
return;
}
if (tr == null) {
response.setContentType("image/png");
response.setIntHeader("Content-Length", blankpng.length);
OutputStream os = response.getOutputStream();
os.write(blankpng);
return;
@ -100,7 +116,6 @@ public class MapStorageResourceHandler extends AbstractHandler {
// Got tile, package up for response
response.setDateHeader("Last-Modified", tr.lastModified);
response.setIntHeader("Content-Length", tr.image.length());
response.setHeader("ETag", "\"" + tr.hashCode + "\"");
if (tr.format == ImageEncoding.PNG) {
response.setContentType("image/png");
}

View File

@ -740,20 +740,27 @@ DynMap.prototype = {
if(tile == null) {
var url = me.options.url.tiles;
if(url.indexOf('?') > 0)
tile = this.registeredTiles[tileName] = url + escape(me.world.name + '/' + tileName) + '&ts=' + me.inittime;
else
tile = this.registeredTiles[tileName] = url + me.world.name + '/' + tileName + '?' + me.inittime;
tile = this.registeredTiles[tileName] = url + escape(me.world.name + '/' + tileName);
}
return tile;
},
onTileUpdated: function(tileName,timestamp) {
var me = this;
var prev = this.registeredTiles[tileName];
var a_b = true;
if (prev && (prev.indexOf('upd=0') > 0))
a_b = false;
var url = me.options.url.tiles;
if(url.indexOf('?') > 0)
this.registeredTiles[tileName] = url + escape(me.world.name + '/' + tileName) + '&ts=' + timestamp;
if (a_b) {
if (url.indexOf('?') > 0) {
this.registeredTiles[tileName] = url + escape(me.world.name + '/' + tileName) + '&upd=0';
}
else {
this.registeredTiles[tileName] = url + escape(me.world.name + '/' + tileName) + '?upd=0';
}
}
else
this.registeredTiles[tileName] = url + me.world.name + '/' + tileName + '?' + timestamp;
this.registeredTiles[tileName] = url + me.world.name + '/' + tileName;
me.maptype.updateNamedTile(tileName);
},
addPlayer: function(update) {