mirror of
https://github.com/Joxit/docker-registry-ui.git
synced 2025-04-29 00:19:54 +03:00
perf: add http cache for blobs and manifests sha256
This commit is contained in:
parent
9cfb6791f8
commit
684f82f24e
2 changed files with 39 additions and 0 deletions
32
src/scripts/cache-request.js
Normal file
32
src/scripts/cache-request.js
Normal 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) {}
|
||||||
|
};
|
|
@ -15,6 +15,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { getFromCache, setCache } from './cache-request';
|
||||||
|
|
||||||
export class Http {
|
export class Http {
|
||||||
constructor(opts) {
|
constructor(opts) {
|
||||||
this.oReq = new XMLHttpRequest();
|
this.oReq = new XMLHttpRequest();
|
||||||
|
@ -78,6 +80,7 @@ export class Http {
|
||||||
req.send();
|
req.send();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
this.status === 200 && setCache(self._method, self._url, this.responseText);
|
||||||
f.bind(this)();
|
f.bind(this)();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -116,6 +119,10 @@ export class Http {
|
||||||
}
|
}
|
||||||
|
|
||||||
send() {
|
send() {
|
||||||
|
const responseText = getFromCache(this._method, this._url);
|
||||||
|
if (responseText) {
|
||||||
|
return this._events['loadend'].bind({ status: 200, responseText })();
|
||||||
|
}
|
||||||
this.oReq.send();
|
this.oReq.send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue