transitory
Version:
In-memory cache with high hit rates via LFU eviction. Supports time-based expiration, automatic loading and metrics.
57 lines (56 loc) • 1.88 kB
TypeScript
import { CacheNode } from '../CacheNode';
import { KeyType } from '../KeyType';
import { Expirable } from './Expirable';
export declare type EvictionListener<K extends KeyType> = (keys: K[]) => void;
/**
* A timer wheel for variable expiration of items in a cache. Stores items in
* layers that are circular buffers that represent a time span.
*
* This implementation takes some extra care to work with Number as they are
* actually doubles and shifting turns them into 32-bit ints. To represent
* time we need more than 32-bits so to fully support things this implementation
* uses a base which is removed from all of the numbers to make them fit into
* 32-bits.
*
* Based on an idea by Ben Manes implemented in Caffeine.
*/
export declare class TimerWheel<K extends KeyType, V> {
private evict;
private time;
private base;
private layers;
constructor(evict: EvictionListener<K>);
get localTime(): number;
private findBucket;
advance(localTime?: number): void;
/**
* Create a node that that helps with tracking when a key and value
* should be evicted.
*
* @param key -
* key to set
* @param value -
* value to set
* @returns
* node
*/
node(key: K, value: V): TimerNode<K, V>;
/**
* Schedule eviction of the given node at the given timestamp.
*
* @param node -
* node to reschedule
* @param time -
* new expiration time
* @returns
* if the node was rescheduled
*/
schedule(node: TimerNode<K, V>, time: number): boolean;
deschedule(node: TimerNode<K, V>): void;
}
export declare class TimerNode<K extends KeyType, V> extends CacheNode<K, V> implements Expirable<V> {
private wheel;
time: number;
constructor(wheel: TimerWheel<K, V>, key: K | null, value: V | null);
isExpired(): boolean;
}