From 8d1221fcbb8f107e2af85c92b018978c3b53b9bc Mon Sep 17 00:00:00 2001 From: fupeng Date: Tue, 15 Feb 2022 01:19:18 +0800 Subject: [PATCH] feat(timeout-map): cleanup priority --- src/object/timeout-map.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/object/timeout-map.ts b/src/object/timeout-map.ts index 2c35e41..4ed05e8 100644 --- a/src/object/timeout-map.ts +++ b/src/object/timeout-map.ts @@ -6,6 +6,11 @@ type TimeoutMapConstructorOptions = { * @default undefined not Control */ maxLength?: number; + /** + * Priority cleaning type + * @default filo + */ + clearUpPriority?: 'shortPeriod' | 'longPeriod' | 'fifo' | 'filo'; }; type TimeoutMapOptions = TimeoutMapConstructorOptions & { @@ -50,6 +55,7 @@ class TimeoutMap extends Map { if (options) { this._options = { passiveDeletion: true, + clearUpPriority: 'filo', ...options, }; } @@ -165,10 +171,29 @@ class TimeoutMap extends Map { } private _cleanOverLimitElement() { - // TODO Prioritize the clearing of fast-expiring if (this._options.maxLength !== undefined && super.size > this._options.maxLength) { - const limitKeys = [...super.keys()].slice(0, super.size - this._options.maxLength); - limitKeys.forEach((key) => { + const invalidKeys: K[] = []; + const sortKeys = () => + [...super.keys()].sort( + (a, b) => + (this._keyArgs.get(a)?.expirationTime || 0) - + (this._keyArgs.get(b)?.expirationTime || 0), + ); + switch (this._options.clearUpPriority) { + case 'filo': + invalidKeys.push(...[...super.keys()].slice(this._options.maxLength, super.size)); + break; + case 'fifo': + invalidKeys.push(...[...super.keys()].slice(0, super.size - this._options.maxLength)); + break; + case 'longPeriod': + invalidKeys.push(...sortKeys().slice(this._options.maxLength, super.size)); + break; + case 'shortPeriod': + invalidKeys.push(...sortKeys().slice(0, super.size - this._options.maxLength)); + break; + } + invalidKeys.forEach((key) => { const arg = this._keyArgs.get(key); if (arg) { arg.options?.onOverLimit?.(key, super.get(key)!, this._keyArgs.get(key)!, this);