@creejs/commons-collection
Version:
Commons Collection
108 lines (107 loc) • 3.16 kB
TypeScript
/**
* Not Found Better Implementation in npmjs.com, so do it by ourselves.
*
* Key Points:
* 1. Basic Atom Unit is "tick", 1 tick = 1 tickInterval
* 3. How many milliseconds does 1 Tick represent is defined by "tickInterval"
* 4. How many Ticks does the Wheel own is defined by "tickCount"
*/
export default class TimeWheelCache extends EventEmitter {
/**
* @param {TimeWheelCacheOptions} [options]
*/
constructor(options?: TimeWheelCacheOptions);
get Event(): {
Advance: string;
Expired: string;
};
options: {};
/**
* How many milliseconds does one Tick represent
*/
_tickInterval: number;
/**
* How many Ticks does the Wheel have
*/
_tickCount: number;
/**
* Slots, one Tick owns one Slot to store key and expire timestamp
* @type {Map<any, {value: any, slotIndex: number, expireTimestamp: number}>[]}
*/
_slots: Map<any, {
value: any;
slotIndex: number;
expireTimestamp: number;
}>[];
/**
* Data Cache
* @type {Map<any, {value: any, slotIndex: number, expireTimestamp: number}>}
*/
_cache: Map<any, {
value: any;
slotIndex: number;
expireTimestamp: number;
}>;
_currentSlotIndex: number;
/** @type {NodeJS.Timeout|undefined} */
_timer: NodeJS.Timeout | undefined;
get tickInterval(): number;
get tickCount(): number;
/**
* Max Time to Live, atom unit "millisecond"
* @returns {number}
*/
get maxTtl(): number;
_maxTtl: number | undefined;
assertStarted(): void;
get autoEvictRunning(): boolean;
_startAutoEvict(): void;
_stopAutoEvict(): void;
_advance(): void;
start(): void;
stop(): void;
destroy(): void;
/**
* 1. "key" is not Nil
* 2. "value" is not Nil
* 3. "ttl" must > 0 && <= maxTtl, maxTtl = tickCount * tickInterval
* * > 0: CAN NOT go to past
* * < maxTtl: Wheel is Round, eg.
* * "Hour Wheel" has 24 Hour-Tick-Mark
* * CAN NOT distinguish 24 hours and 48 hours
* 4. Understand the difference between "ttl" and "timestamp"
* * TTL is Indexed from 1
* * eg. ttl = 60, should be stored in Slot[59]
* * Timestamp is Indexed from 0
* @param {any} key
* @param {any} value
* @param {number} ttl Time to Live, atom unit "millisecond"
* @returns {boolean}
*/
set(key: any, value: any, ttl: number): boolean;
/**
* @param {any} key
* @returns {boolean}
*/
delete(key: any): boolean;
/**
* Checks if the cache contains the specified key.
* @param {any} key - The key to check for existence in the cache.
* @returns {boolean} - True if the key exists in the cache, false otherwise.
*/
has(key: any): boolean;
clear(): void;
/**
*
* @param {any} key
* @returns {any}
*/
get(key: any): any;
size(): number;
}
export type Timestamp = number;
export type TimeWheelCacheOptions = {
tickInterval: number;
tickCount: number;
};
import { EventEmitter } from '@creejs/commons-events';