UNPKG

@creejs/commons-collection

Version:
99 lines (98 loc) 3.04 kB
/** * Hour-Minute-Second-Time-Wheel Cache * 1. TTL must be less than 24 Hours */ export default class Hour24TimeWheelCache extends EventEmitter { static get DowngradType(): { HourToSecond: string; HourToMinute: string; MinuteToSecond: string; }; static get Event(): { Downgrade: string; Expired: string; }; /** * Second Wheel: * 1. 1 Tick Mark is 1 Second, is 1000 Milliseconds * 2. 60 Slots, maximumly, SencondWheel can contain 60 Seconds * 3. 60 Seconds should be stored in MinuteWheel * * 01 00:00:00.XXX -> 00:00:59.XXX in Slot01 Index00 * * 02 00:00:01.XXX -> 00:00:01.XXX in Slot02 Index01 * * 60 00:00:59.XXX -> 00:00:59.XXX in Slot60 Index59 * @type {TimeWheelCache} */ _secondWheel: TimeWheelCache; /** * Minute Wheel: * 1. 1 Tick Mark is 1 Minute, is 60 * 1000 Milliseconds * 2. 60 Slots, maximumly, MinuteWheel can contain 60 Minutes * * 01 00:00:00 -> 00:00:59 in Slot01 Index00 * * 02 00:01:00 -> 00:01:59 in Slot02 Index01 * * 60 00:59:00 -> 00:59:59 in Slot60 Index59 * @type {TimeWheelCache} */ _minuteWheel: TimeWheelCache; /** * Hour Wheel: * 1. 1 Tick Mark is 1 Hour, is 60 * 60 * 1000 Milliseconds * 2. 24 Slots, maximumly, HourWheel can contain 23:59:59 * * 01 00:00:00 -> 00:59:59 in Slot01 Index00 * * 02 01:00:00 -> 01:59:59 in Slot02 Index01 * * 24 23:00:00 -> 23:59:59 in Slot23 Index23 * @type {TimeWheelCache} */ _hourWheel: TimeWheelCache; /** * @type {Map<any, TimeWheelCache>} */ _cache: Map<any, TimeWheelCache>; /** * Max Time to Live, atom unit "millisecond" * @returns {number} */ get maxTtl(): number; get autoEvictRunning(): boolean; _init(): void; start(): void; stop(): void; destroy(): void; /** * @param {any} key * @param {any} value * @param {number} ttl Time To Live, unit "millisencond", ttl should < 24 Hours * @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; import { EventEmitter } from '@creejs/commons-events'; import TimeWheelCache from './time-wheel-cache.js'; export namespace DowngradType { let HourToSecond: string; let HourToMinute: string; let MinuteToSecond: string; } export namespace Event { let Downgrade: string; let Expired: string; }