@thermopylae/lib.cache
Version:
85 lines (84 loc) • 2.86 kB
TypeScript
import { Threshold } from '@thermopylae/core.declarations';
import { CacheReplacementPolicy, Deleter, EntryValidity } from '../../contracts/cache-replacement-policy';
import { CacheEntry } from '../../contracts/commons';
import { BucketEntryNode } from '../../data-structures/bucket-list/ordered-bucket-list';
import { CacheBackendElementsCount } from '../../contracts/cache-backend';
/**
* @private
*/
interface EvictableCacheEntry<Key, Value> extends CacheEntry<Key, Value>, BucketEntryNode<EvictableCacheEntry<Key, Value>> {
}
/**
* Base class for LFU policies.
*
* @private
*
* @template Key Type of the key.
* @template Value Type of the value.
* @template ArgumentsBundle Type of the arguments bundle.
*/
declare abstract class BaseLFUEvictionPolicy<Key, Value, ArgumentsBundle> implements CacheReplacementPolicy<Key, Value, ArgumentsBundle> {
private readonly frequencies;
private readonly cacheMaxCapacity;
private readonly cacheBackendElementsCount;
private deleteFromCache;
/**
* @param cacheMaxCapacity {@link Cache} maximum capacity.
* @param cacheBackendElementsCount Cache backend elements count.
*/
constructor(cacheMaxCapacity: Threshold, cacheBackendElementsCount: CacheBackendElementsCount);
/**
* @returns Total number of elements from frequency list.
*/
get size(): number;
/**
* @inheritDoc
*/
onHit(entry: EvictableCacheEntry<Key, Value>): EntryValidity;
/**
* @inheritDoc
*/
onMiss(): void;
/**
* @inheritDoc
*/
onSet(entry: EvictableCacheEntry<Key, Value>): void;
/**
* @inheritDoc
*/
onUpdate(_entry: EvictableCacheEntry<Key, Value>): void;
/**
* @inheritDoc
*/
onDelete(entry: EvictableCacheEntry<Key, Value>): void;
/**
* @inheritDoc
*/
onClear(): void;
/**
* @inheritDoc
*/
setDeleter(deleter: Deleter<Key, Value>): void;
private evict;
/**
* @returns Entry initial starting frequency.
*/
protected abstract get initialFrequency(): number;
/**
* Delegate called before entry needs to be inserted in a frequency bucket. <br/>
* Entry will be inserted in the bucket that has frequency equal to result returned by this function.
*
* @param entry Entry for which score needs to be computed.
* @param entryScore Current score of the entry.
*
* @returns New frequency of the entry.
*/
protected abstract computeEntryFrequency(entry: EvictableCacheEntry<Key, Value>, entryScore: number): number;
/**
* Delegate called after item has been evicted from cache.
*
* @param frequency Frequency of the evicted item.
*/
protected abstract onEvict(frequency: number): void;
}
export { BaseLFUEvictionPolicy, EvictableCacheEntry };