mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-07 23:47:53 +08:00
f699575b45
* Cache mermaid graph rendering * Bump mermaid * Update naming
33 lines
728 B
JavaScript
33 lines
728 B
JavaScript
/**
|
|
* A Map-based LRU cache.
|
|
*/
|
|
export default class CacheLRU {
|
|
constructor(size) {
|
|
this.size = size;
|
|
this.cache = new Map();
|
|
}
|
|
|
|
get(key) {
|
|
if (this.cache.has(key)) {
|
|
const value = this.cache.get(key);
|
|
// Map keys are stored and iterated in insertion order,
|
|
// so we reinsert on every access
|
|
this.cache.delete(key);
|
|
this.cache.set(key, value);
|
|
return value;
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
set(key, value) {
|
|
if (this.cache.has(key)) {
|
|
this.cache.delete(key);
|
|
} else if (this.cache.size === this.size) {
|
|
const oldestKey = this.cache.keys().next().value;
|
|
this.cache.delete(oldestKey);
|
|
}
|
|
|
|
this.cache.set(key, value);
|
|
}
|
|
}
|