From bfea4dbf5f119a1be3f120397fd1763698855de5 Mon Sep 17 00:00:00 2001 From: "R. Aidan Campbell" Date: Sat, 3 Sep 2016 13:51:56 -0400 Subject: [PATCH] added cache warming on startup --- static/js/index.js | 3 +-- static/js/utils.js | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/static/js/index.js b/static/js/index.js index 4a59148..c5a7b7a 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -74,9 +74,9 @@ function restoreUiAfterNaClLoad() { } function beginBackgroundPollingOfHost(host) { + host.warmBoxArtCache(); if (host.online) { $("#hostgrid-" + host.serverUid).removeClass('host-cell-inactive'); - // The host was already online. Just start polling in the background now. activePolls[host.serverUid] = window.setInterval(function() { // every 5 seconds, poll at the address we know it was live at @@ -90,7 +90,6 @@ function beginBackgroundPollingOfHost(host) { }, 5000); } else { $("#hostgrid-" + host.serverUid).addClass('host-cell-inactive'); - // The host was offline, so poll immediately. host.pollServer(function () { if (host.online) { diff --git a/static/js/utils.js b/static/js/utils.js index eb554d6..8edac61 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -293,12 +293,35 @@ NvHTTP.prototype = { return this.getAppListWithCacheFlush(); }, + + warmBoxArtCache: function () { + if (Object.keys(this._memCachedBoxArtArray).length != 0) { + console.log('box art cache already warmed.'); + return; + } + if (chrome.storage) { + chrome.storage.local.get('boxArtCache', function(JSONCachedBoxArtArray) { + + var storedBoxArtArray; // load cached data if it exists + if (JSONCachedBoxArtArray.boxArtCache != undefined) { + storedBoxArtArray = JSONCachedBoxArtArray.boxArtCache; + for (var key in storedBoxArtArray) { + this._memCachedBoxArtArray[key] = _base64ToArrayBuffer(storedBoxArtArray[key]); + } + console.log('box art cache warmed.'); + } else { + console.log('WARN: no box art found in storage. Cannot warm cache!'); + return; + } + }.bind(this)); + } + }, // returns the box art of the given appID. // three layers of response time are possible: memory cached (in javascript), storage cached (in chrome.storage.local), and streamed (host sends binary over the network) getBoxArt: function (appId) { - // TODO: unfortunately we do N lookups from storage cache, each of them filling up the memory cache. + // TODO: unfortunately we do N lookups from storage cache, each of them filling up the memory cache. // once the first round of calls are all made, each subsequent request hits this and returns from memory cache if (this._memCachedBoxArtArray[appId] !== undefined) { return new Promise(function (resolve, reject) {