UNPKG

s2maps-gpu

Version:

S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.

71 lines (70 loc) 2.27 kB
/** Cache that keeps the most recently used items and deletes the least recently used items */ export default class Cache extends Map { maxCacheSize; #order = []; /** @param maxCacheSize - the maximum size of the cache */ constructor(maxCacheSize = 100) { super(); this.maxCacheSize = maxCacheSize; } /** * Set a value in the cache. If the cache is full, the least recently used item will be deleted * @param key - the key * @param value - the value * @returns sets the key-value and returns the cache */ set(key, value) { // place in front the new this.#order.unshift(key); while (this.#order.length > this.maxCacheSize) this.delete(this.#order[this.#order.length - 1]); return super.set(key, value); } /** * Get a value from the cache. If the value exists, it will be placed in the front of the cache * @param key - the key * @returns the value or undefined if the value does not exist */ get(key) { // update the place in the array and than get this.#order.splice(this.#order.indexOf(key), 1); this.#order.unshift(key); return super.get(key); } /** * Get a batch of values from the cache. If the value exists, it will be placed in the front of the cache * @param keys - the keys to get * @returns the values */ getBatch(keys) { const values = []; for (const key of keys) { const value = this.get(key); if (value !== undefined) values.push(value); } return values; } /** @returns all values from the cache */ getAll() { return this.getBatch(this.#order); } /** * Delete a value from the cache * @param key - the key to delete * @returns true if the value was deleted */ delete(key) { this.#order.splice(this.#order.indexOf(key), 1); const value = super.get(key); value?.delete?.(); return super.delete(key); } /** Delete all values from the cache */ deleteAll() { for (const [, value] of this) value?.delete?.(); super.clear(); this.#order = []; } }