mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2024-06-07 21:20:49 +00:00
60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
// Prevent eslint errors on functions defined in other files.
|
|
/*global
|
|
isNumberThrowError,
|
|
isNullOrUndefined,
|
|
*/
|
|
/*eslint no-undef: "error"*/
|
|
|
|
const LRU_CACHE_MAX_ITEMS_DEFAULT = 250;
|
|
class LRUCache {
|
|
/** Least Recently Used cache implementation.
|
|
*
|
|
* Source: https://stackoverflow.com/a/46432113
|
|
*/
|
|
constructor(max = LRU_CACHE_MAX_ITEMS_DEFAULT) {
|
|
isNumberThrowError(max);
|
|
|
|
this.max = max;
|
|
this.cache = new Map();
|
|
}
|
|
|
|
clear() {
|
|
this.cache.clear();
|
|
}
|
|
|
|
destroy() {
|
|
this.clear();
|
|
this.cache = null;
|
|
}
|
|
|
|
size() {
|
|
return this.cache.size;
|
|
}
|
|
|
|
get(key) {
|
|
let item = this.cache.get(key);
|
|
if (!isNullOrUndefined(item)) {
|
|
this.cache.delete(key);
|
|
this.cache.set(key, item);
|
|
}
|
|
return item;
|
|
}
|
|
|
|
set(key, val) {
|
|
if (this.cache.has(key)) {
|
|
this.cache.delete(key);
|
|
} else if (this.cache.size === this.max) {
|
|
this.cache.delete(this.first());
|
|
}
|
|
this.cache.set(key, val);
|
|
}
|
|
|
|
has(key) {
|
|
return this.cache.has(key);
|
|
}
|
|
|
|
first() {
|
|
return this.cache.keys().next().value;
|
|
}
|
|
}
|