perf: add http cache for blobs and manifests sha256

This commit is contained in:
Joxit 2023-06-04 09:17:22 +02:00
parent 9cfb6791f8
commit 684f82f24e
No known key found for this signature in database
GPG Key ID: F526592B8E012263
2 changed files with 39 additions and 0 deletions

View File

@ -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) {}
};

View File

@ -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();
@ -78,6 +80,7 @@ export class Http {
req.send();
});
} else {
this.status === 200 && setCache(self._method, self._url, this.responseText);
f.bind(this)();
}
});
@ -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();
}
}