UNPKG

transitory

Version:

In-memory cache with high hit rates via LFU eviction. Supports time-based expiration, automatic loading and metrics.

169 lines 4.89 kB
import { AbstractCache } from './AbstractCache'; import { ON_REMOVE, ON_MAINTENANCE, TRIGGER_REMOVE } from './symbols'; const PARENT = Symbol('parent'); const REMOVAL_LISTENER = Symbol('removalListener'); /** * Wrapper for another cache, used to extend that cache with new behavior, * like for loading things or collecting metrics. */ export class WrappedCache extends AbstractCache { constructor(parent, removalListener) { super(); this[PARENT] = parent; this[REMOVAL_LISTENER] = removalListener; // Custom onRemove handler for the parent cache this[PARENT][ON_REMOVE] = this[TRIGGER_REMOVE].bind(this); } /** * The maximum size the cache can be. Will be -1 if the cache is unbounded. * * @returns * maximum size */ get maxSize() { return this[PARENT].maxSize; } /** * The current size of the cache. * * @returns * current size */ get size() { return this[PARENT].size; } /** * The size of the cache weighted via the activate estimator. * * @returns * current weighted size */ get weightedSize() { return this[PARENT].weightedSize; } /** * Store a value tied to the specified key. Returns the previous value or * `null` if no value currently exists for the given key. * * @param key - * key to store value under * @param value - * value to store * @returns * current value or `null` */ set(key, value) { return this[PARENT].set(key, value); } /** * Get the cached value for the specified key if it exists. Will return * the value or `null` if no cached value exist. Updates the usage of the * key. * * @param key - * key to get * @returns * current value or `null` */ getIfPresent(key) { return this[PARENT].getIfPresent(key); } /** * Peek to see if a key is present without updating the usage of the * key. Returns the value associated with the key or `null` if the key * is not present. * * In many cases `has(key)` is a better option to see if a key is present. * * @param key - * the key to check * @returns * value associated with key or `null` */ peek(key) { return this[PARENT].peek(key); } /** * Check if the given key exists in the cache. * * @param key - * key to check * @returns * `true` if value currently exists, `false` otherwise */ has(key) { return this[PARENT].has(key); } /** * Delete a value in the cache. Returns the deleted value or `null` if * there was no value associated with the key in the cache. * * @param key - * the key to delete * @returns * deleted value or `null` */ delete(key) { return this[PARENT].delete(key); } /** * Clear the cache removing all of the entries cached. */ clear() { this[PARENT].clear(); } /** * Get all of the keys in the cache as an array. Can be used to iterate * over all of the values in the cache, but be sure to protect against * values being removed during iteration due to time-based expiration if * used. * * @returns * snapshot of keys */ keys() { return this[PARENT].keys(); } /** * Request clean up of the cache by removing expired entries and * old data. Clean up is done automatically a short time after sets and * deletes, but if your cache uses time-based expiration and has very * sporadic updates it might be a good idea to call `cleanUp()` at times. * * A good starting point would be to call `cleanUp()` in a `setInterval` * with a delay of at least a few minutes. */ cleanUp() { this[PARENT].cleanUp(); } /** * Get metrics for this cache. Returns an object with the keys `hits`, * `misses` and `hitRate`. For caches that do not have metrics enabled * trying to access metrics will throw an error. * * @returns * metrics */ get metrics() { return this[PARENT].metrics; } get [ON_MAINTENANCE]() { return this[PARENT][ON_MAINTENANCE]; } set [ON_MAINTENANCE](listener) { this[PARENT][ON_MAINTENANCE] = listener; } [TRIGGER_REMOVE](key, value, reason) { // Trigger any extended remove listeners const onRemove = this[ON_REMOVE]; if (onRemove) { onRemove(key, value, reason); } // Trigger the removal listener const listener = this[REMOVAL_LISTENER]; if (listener) { listener(key, value, reason); } } } //# sourceMappingURL=WrappedCache.js.map