UNPKG

@thermopylae/lib.cache

Version:
74 lines (73 loc) 3.26 kB
/// <reference types="node" /> import { MaybePromise, Undefinable } from '@thermopylae/core.declarations'; import { EventEmitter } from 'events'; import { CacheBackend } from '../contracts/cache-backend'; import { CacheReplacementPolicy } from '../contracts/cache-replacement-policy'; import { Cache, CacheEvent, CacheEventListener } from '../contracts/cache'; /** * {@link Cache} implementation which uses {@link CacheReplacementPolicy} for keys eviction. <br/> * Although any predefined policy can be used, there are some restrictions for multiple policies combination. <br/> * You can combine only 1 policy from each category: * * Category | Policies * ---------------- | ----------------------------- * Expiration | - {@link ProactiveExpirationPolicy}<br/>- {@link ReactiveExpirationPolicy}<br/>- {@link SlidingProactiveExpirationPolicy}<br/>- {@link SlidingReactiveExpirationPolicy} * Eviction | - {@link ArcEvictionPolicy}<br/>- {@link LRUEvictionPolicy}<br/>- {@link SegmentedLRUEvictionPolicy}<br/>- {@link LFUEvictionPolicy}<br/>- {@link LFUDAEvictionPolicy}<br/>- {@link GDSFEvictionPolicy} * Priority | - {@link PriorityEvictionPolicy} * Dependencies | - {@link KeysDependenciesEvictionPolicy} * * For example, the following combination is a valid one: [{@link ProactiveExpirationPolicy}, {@link LRUEvictionPolicy}, {@link KeysDependenciesEvictionPolicy}].<br/> * While the following: [{@link ProactiveExpirationPolicy}, {@link SlidingProactiveExpirationPolicy}] isn't, because it contains 2 policies from same category. * * @template Key Type of the key. * @template Value Type of the value. * @template ArgumentsBundle Type of the arguments bundle. */ declare class PolicyBasedCache<Key, Value, ArgumentsBundle = unknown> extends EventEmitter implements Cache<Key, Value, ArgumentsBundle> { private readonly backend; private readonly policies; /** * @param backend Cache backend. * @param policies Array of policies. <br/> * If you pass nothing or an empty array, cache will act as a simple wrapper over backend. */ constructor(backend: CacheBackend<Key, Value>, policies?: Array<CacheReplacementPolicy<Key, Value, ArgumentsBundle>>); /** * Get number of cache entries. */ get size(): number; /** * @inheritDoc */ get(key: Key): Undefinable<Value>; /** * Check whether **key** is present in the cache, without calling policies *onHit* hook. <br/> * Notice, that some policies might evict item when *onHit* hook is called (e.g. item expired), * therefore even if method returns **true**, trying to *get* item might evict him and you will get `undefined` as result. * * @param key Name of the key. */ has(key: Key): boolean; /** * @inheritDoc */ set(key: Key, value: Value, argsBundle?: ArgumentsBundle): void; /** * @inheritDoc */ del(key: Key): boolean; /** * @inheritDoc */ keys(): Array<Key>; /** * @inheritDoc */ clear(): void; /** * @inheritDoc */ on(event: CacheEvent, listener: CacheEventListener<Key, MaybePromise<Value, 'plain'>>): this; private internalDelete; } export { PolicyBasedCache };