From 2750100d24e0be9ccc919b565dbae67ea4fcab7a Mon Sep 17 00:00:00 2001 From: Jirka Date: Tue, 29 Mar 2022 09:30:25 +0200 Subject: [PATCH 1/3] Move cache-key generation to TileSource: user might need to override default behaviour in case of non-unique URL's (post data used). --- src/tile.js | 9 +++------ src/tiledimage.js | 3 ++- src/tilesource.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/tile.js b/src/tile.js index a6d40fd8..582ccb67 100644 --- a/src/tile.js +++ b/src/tile.js @@ -54,8 +54,9 @@ * with HTML the entire tile is always used. * @param {String} postData HTTP POST data (usually but not necessarily in k=v&k2=v2... form, * see TileSrouce::getPostData) or null + * @param {String} cacheKey key to act as a tile cache, must be unique for tiles with unique image data */ -$.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, ajaxHeaders, sourceBounds, postData) { +$.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, ajaxHeaders, sourceBounds, postData, cacheKey) { /** * The zoom level this tile belongs to. * @member {Number} level @@ -131,11 +132,7 @@ $.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, aja * @member {String} cacheKey * @memberof OpenSeadragon.Tile# */ - if (this.ajaxHeaders) { - this.cacheKey = this.url + "+" + JSON.stringify(this.ajaxHeaders); - } else { - this.cacheKey = this.url; - } + this.cacheKey = cacheKey; /** * Is this tile loaded? * @member {Boolean} loaded diff --git a/src/tiledimage.js b/src/tiledimage.js index afb5d48c..f1737425 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -1530,7 +1530,8 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag this.loadTilesWithAjax, ajaxHeaders, sourceBounds, - post + post, + tileSource.getTileHashKey(level, xMod, yMod, url, ajaxHeaders, post) ); if (this.getFlip()) { diff --git a/src/tilesource.js b/src/tilesource.js index 48cfe851..7a7e991c 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -639,7 +639,11 @@ $.TileSource.prototype = { * let result = new FormData(); * result.append("data", myData); * return result; - + * + * IMPORTANT: in case you move all the logic on image fetching + * to post data, you must re-define 'getTileHashKey(...)' to + * stay unique for different tile images. + * * @param level * @param x * @param y @@ -666,6 +670,29 @@ $.TileSource.prototype = { return {}; }, + /** + * Most tiles are cached because their 'context2D' property is null (otherwise no caching occurs). + * Then, their cache is uniquely determined by this key: this key should be different if images + * are different! Note: default behaviour does not take into account post data. + * + * A tile can have either context2D defined (TileSource.prototype.getContext2D) + * or it's context2D is set manually. In those cases cache is not used and this function + * is irrelevant. + * @param level tile level it was fetched with + * @param x x-coordinate in the pyramid level + * @param y y-coordinate in the pyramid level + * @param url the tile was fetched with + * @param ajaxHeaders the tile was fetched with + * @param post data the tile was fetched with + */ + getTileHashKey: function(level, x, y, url, ajaxHeaders, post) { + if (ajaxHeaders) { + return url + "+" + JSON.stringify(ajaxHeaders); + } else { + return url; + } + }, + /** * @function * @param {Number} level From ee2404afe35d46359f6edead98fe804cbd95c1a1 Mon Sep 17 00:00:00 2001 From: Jirka Date: Thu, 7 Apr 2022 14:48:26 +0200 Subject: [PATCH 2/3] Update Tile.prototype.getTileHashKey() documentation. Provide deprecation warning for cacheKey property - now it must be passed through Tile constructor. --- src/tile.js | 6 ++++++ src/tilesource.js | 14 ++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/tile.js b/src/tile.js index 582ccb67..9d29028a 100644 --- a/src/tile.js +++ b/src/tile.js @@ -132,7 +132,13 @@ $.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, aja * @member {String} cacheKey * @memberof OpenSeadragon.Tile# */ + if (cacheKey === undefined) { + $.console.error("Tile constructor needs 'cacheKey' variable: creation tile cache" + + " in Tile class is deprecated. TileSource.prototype.getTileHashKey will be used."); + cacheKey = $.TileSource.prototype.getTileHashKey(level, x, y, url, ajaxHeaders, postData); + } this.cacheKey = cacheKey; + /** * Is this tile loaded? * @member {Boolean} loaded diff --git a/src/tilesource.js b/src/tilesource.js index 7a7e991c..36316a1d 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -671,21 +671,19 @@ $.TileSource.prototype = { }, /** - * Most tiles are cached because their 'context2D' property is null (otherwise no caching occurs). - * Then, their cache is uniquely determined by this key: this key should be different if images - * are different! Note: default behaviour does not take into account post data. - * * A tile can have either context2D defined (TileSource.prototype.getContext2D) - * or it's context2D is set manually. In those cases cache is not used and this function - * is irrelevant. + * or its context2D is set manually. In those cases cache is not used and this function + * is irrelevant. Otherwise, the tile cache object is uniquely determined by this key: + * keys should be different if images are different! + * Note: default behaviour does not take into account post data. * @param level tile level it was fetched with * @param x x-coordinate in the pyramid level * @param y y-coordinate in the pyramid level * @param url the tile was fetched with * @param ajaxHeaders the tile was fetched with - * @param post data the tile was fetched with + * @param postData data the tile was fetched with */ - getTileHashKey: function(level, x, y, url, ajaxHeaders, post) { + getTileHashKey: function(level, x, y, url, ajaxHeaders, postData) { if (ajaxHeaders) { return url + "+" + JSON.stringify(ajaxHeaders); } else { From 3a84b9369b72b8c05edf3cab4ce2315268e0853b Mon Sep 17 00:00:00 2001 From: Jirka Date: Wed, 13 Apr 2022 10:47:48 +0200 Subject: [PATCH 3/3] Clarify documentation on cache-key creation. --- src/tile.js | 6 +++--- src/tilesource.js | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/tile.js b/src/tile.js index 9d29028a..614827ac 100644 --- a/src/tile.js +++ b/src/tile.js @@ -52,7 +52,7 @@ * @param {OpenSeadragon.Rect} sourceBounds The portion of the tile to use as the source of the * drawing operation, in pixels. Note that this only works when drawing with canvas; when drawing * with HTML the entire tile is always used. - * @param {String} postData HTTP POST data (usually but not necessarily in k=v&k2=v2... form, + * @param {String} postData HTTP POST data (usually but not necessarily in k=v&k2=v2... form, * see TileSrouce::getPostData) or null * @param {String} cacheKey key to act as a tile cache, must be unique for tiles with unique image data */ @@ -101,8 +101,8 @@ $.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, aja */ this.url = url; /** - * Post parameters for this tile. Either it is an URL-encoded string - * in k1=v1&k2=v2... format or null + * Post parameters for this tile. For example, it can be an URL-encoded string + * in k1=v1&k2=v2... format, or a JSON, or a FormData instance... or null if no POST request used * @member {String} postData HTTP POST data (usually but not necessarily in k=v&k2=v2... form, * see TileSrouce::getPostData) or null * @memberof OpenSeadragon.Tile# diff --git a/src/tilesource.js b/src/tilesource.js index 36316a1d..e6cc9b1f 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -644,9 +644,9 @@ $.TileSource.prototype = { * to post data, you must re-define 'getTileHashKey(...)' to * stay unique for different tile images. * - * @param level - * @param x - * @param y + * @param {Number} level + * @param {Number} x + * @param {Number} y * @return {* || null} post data to send with tile configuration request */ getTilePostData: function( level, x, y ) { @@ -671,17 +671,19 @@ $.TileSource.prototype = { }, /** - * A tile can have either context2D defined (TileSource.prototype.getContext2D) - * or its context2D is set manually. In those cases cache is not used and this function - * is irrelevant. Otherwise, the tile cache object is uniquely determined by this key: - * keys should be different if images are different! + * The tile cache object is uniquely determined by this key and used to lookup + * the image data in cache: keys should be different if images are different. + * + * In case a tile has context2D property defined (TileSource.prototype.getContext2D) + * or its context2D is set manually; the cache is not used and this function + * is irrelevant. * Note: default behaviour does not take into account post data. - * @param level tile level it was fetched with - * @param x x-coordinate in the pyramid level - * @param y y-coordinate in the pyramid level - * @param url the tile was fetched with - * @param ajaxHeaders the tile was fetched with - * @param postData data the tile was fetched with + * @param {Number} level tile level it was fetched with + * @param {Number} x x-coordinate in the pyramid level + * @param {Number} y y-coordinate in the pyramid level + * @param {String} url the tile was fetched with + * @param {Object} ajaxHeaders the tile was fetched with + * @param {*} postData data the tile was fetched with (type depends on getTilePostData(..) return type) */ getTileHashKey: function(level, x, y, url, ajaxHeaders, postData) { if (ajaxHeaders) {