Skip to content

Commit

Permalink
perf: add http cache for blobs and manifests sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Jun 6, 2023
1 parent 9cfb679 commit 684f82f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/scripts/cache-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const SHA_REGEX = /(blobs|manifests)\/sha256:[a-f0-9]+$/;

const getSha256 = (method, url) => {
if (method !== 'GET') {
return;
}
const parts = SHA_REGEX.exec(url);
if (!parts || !parts[0]) {
return;
}
return parts[0];
};

export const getFromCache = (method, url) => {
const sha256 = getSha256(method, url);
if (!sha256) {
return;
}
try {
return sessionStorage.getItem(sha256);
} catch (e) {}
};

export const setCache = (method, url, responseText) => {
const sha256 = getSha256(method, url);
if (!sha256) {
return;
}
try {
sessionStorage.setItem(sha256, responseText);
} catch (e) {}
};
7 changes: 7 additions & 0 deletions src/scripts/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getFromCache, setCache } from './cache-request';

export class Http {
constructor(opts) {
this.oReq = new XMLHttpRequest();
Expand Down Expand Up @@ -78,6 +80,7 @@ export class Http {
req.send();
});
} else {
this.status === 200 && setCache(self._method, self._url, this.responseText);
f.bind(this)();
}
});
Expand Down Expand Up @@ -116,6 +119,10 @@ export class Http {
}

send() {
const responseText = getFromCache(this._method, this._url);
if (responseText) {
return this._events['loadend'].bind({ status: 200, responseText })();
}
this.oReq.send();
}
}
Expand Down

0 comments on commit 684f82f

Please sign in to comment.