@thermopylae/lib.cache
Version:
84 lines (83 loc) • 2.55 kB
TypeScript
import { Nullable, Percentage, Threshold } from '@thermopylae/core.declarations';
import { CacheReplacementPolicy, Deleter, EntryValidity } from '../../contracts/cache-replacement-policy';
import { CacheEntry } from '../../contracts/commons';
import { DoublyLinkedListNode } from '../../data-structures/list/doubly-linked';
/**
* @private
*/
declare const SEGMENT_SYM: unique symbol;
/**
* @private
*/
declare const enum SegmentType {
PROBATION = 0,
PROTECTED = 1
}
/**
* @private
*/
interface EvictableCacheEntry<Key, Value> extends CacheEntry<Key, Value>, DoublyLinkedListNode<EvictableCacheEntry<Key, Value>> {
[SEGMENT_SYM]: SegmentType;
}
/**
* [Segmented LRU](https://en.wikipedia.org/wiki/Cache_replacement_policies#Segmented_LRU_(SLRU) "Segmented LRU (SLRU)") eviction policy.
*
* @template Key Type of the key.
* @template Value Type of the value.
* @template ArgumentsBundle Type of the arguments bundle.
*/
declare class SegmentedLRUEvictionPolicy<Key, Value, ArgumentsBundle> implements CacheReplacementPolicy<Key, Value, ArgumentsBundle> {
private readonly segments;
private deleteFromCache;
/**
* @param cacheMaxCapacity {@link Cache} maximum capacity.
* @param protectedOverProbationRatio Size of protected segment expressed in % from `cacheMaxCapacity`.<br/>
* Defaults to 70%.
*/
constructor(cacheMaxCapacity: Threshold, protectedOverProbationRatio?: Percentage);
/**
* Get the number of the elements stored in internal structures of this policy.
*/
get size(): number;
/**
* Get most recently used key.
*/
get mostRecent(): Nullable<EvictableCacheEntry<Key, Value>>;
/**
* Get least recently used key.
*/
get leastRecent(): Nullable<EvictableCacheEntry<Key, Value>>;
/**
* @inheritDoc
*/
onHit(entry: EvictableCacheEntry<Key, Value>): EntryValidity;
/**
* @inheritDoc
*/
onMiss(): void;
/**
* @inheritDoc
*/
onSet(entry: EvictableCacheEntry<Key, Value>): void;
/**
* @inheritDoc
*/
onUpdate(): void;
/**
* @inheritDoc
*/
onDelete(entry: EvictableCacheEntry<Key, Value>): void;
/**
* @inheritDoc
*/
onClear(): void;
/**
* @inheritDoc
*/
setDeleter(deleter: Deleter<Key, Value>): void;
private isFull;
private demote;
private promote;
private getEntryFrom;
}
export { SegmentedLRUEvictionPolicy, EvictableCacheEntry, SegmentType, SEGMENT_SYM };