From a376aa91f908439e725fc0211e1f368d9a8de89e Mon Sep 17 00:00:00 2001 From: renxia Date: Fri, 2 Feb 2024 11:05:20 +0800 Subject: [PATCH] =?UTF-8?q?perf(LRUCache):=20=E6=94=B9=E8=BF=9B=20ts=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=A3=B0=E6=98=8E=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 16 ++++++++-------- src/common/lib/LRUCache.ts | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index a35b7ad..56d56fb 100644 --- a/package.json +++ b/package.json @@ -65,27 +65,27 @@ "@iarna/toml": "^2.2.5", "@jest/core": "^29", "@jest/types": "^29", - "@lzwme/fed-lint-helper": "^2.5.1", + "@lzwme/fed-lint-helper": "^2.5.2", "@types/eslint": "^8.56.2", - "@types/jest": "^29.5.11", + "@types/jest": "^29.5.12", "@types/micromatch": "^4.0.6", - "@types/node": "^20.11.3", - "@typescript-eslint/eslint-plugin": "^6.19.0", - "@typescript-eslint/parser": "^6.19.0", + "@types/node": "^20.11.16", + "@typescript-eslint/eslint-plugin": "^6.20.0", + "@typescript-eslint/parser": "^6.20.0", "compressing": "^1.10.0", "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-jest": "^27.6.3", "eslint-plugin-prettier": "^5.1.3", "eslint-plugin-unicorn": "^50.0.1", - "husky": "^8.0.3", + "husky": "^9.0.10", "jest": "^29.7.0", "json5": "^2.2.3", "micromatch": "^4.0.5", "npm-run-all": "^4.1.5", - "prettier": "^3.2.2", + "prettier": "^3.2.4", "standard-version": "^9.5.0", - "ts-jest": "^29.1.1", + "ts-jest": "^29.1.2", "ts-node": "^10.9.2", "typedoc": "^0.25.7", "typescript": "^5.3.3", diff --git a/src/common/lib/LRUCache.ts b/src/common/lib/LRUCache.ts index 92a7e92..f59cfb1 100644 --- a/src/common/lib/LRUCache.ts +++ b/src/common/lib/LRUCache.ts @@ -1,25 +1,25 @@ // @see https://www.npmjs.com/package/lru-cache -export interface LRUCacheOptions { +export interface LRUCacheOptions { /** The maximum number of items that remain in the cache */ max?: number; /** how long to live in ms */ ttl?: number; updateAgeOnGet?: boolean; /** Function that is called on items when they are dropped from the cache */ - dispose?(val: unknown, key: string, reason: string): void; + dispose?(val: LRUCacheItem, key: K, reason: string): void; } -interface LRUCacheItem { +interface LRUCacheItem { t: number; ttl: number; - v: unknown; + v: V; } -export class LRUCache { - protected options: Required; - protected cache = new Map(); - constructor(options: LRUCacheOptions) { +export class LRUCache { + protected options: Required>; + protected cache = new Map>(); + constructor(options: LRUCacheOptions) { options = { ttl: 0, dispose: () => void 0, @@ -31,7 +31,7 @@ export class LRUCache { this.options = options as never; } /** Add a value to the cache */ - set(key: string, value: T, opts?: { ttl?: number }) { + set(key: K, value: T, opts?: { ttl?: number }) { const { cache } = this; if (cache.has(key)) this.delete(key, 'set'); @@ -48,7 +48,7 @@ export class LRUCache { } } /** Return a value from the cache */ - get(key: string, options: { updateAgeOnGet?: boolean } = {}) { + get(key: K, options: { updateAgeOnGet?: boolean } = {}) { const cache = this.cache; if (!cache.has(key)) return; const value = cache.get(key)!; @@ -58,18 +58,18 @@ export class LRUCache { return; } - if (this.options.updateAgeOnGet || options.updateAgeOnGet) value.t = Date.now(); + if (options.updateAgeOnGet || (this.options.updateAgeOnGet && options.updateAgeOnGet !== false)) value.t = Date.now(); cache.delete(key); cache.set(key, value); return value.v as T; } /** Deletes a key out of the cache */ - delete(key: string, reason = 'delete') { + delete(key: K, reason = 'delete') { if (!this.cache.has(key)) return false; const val = this.cache.get(key); this.cache.delete(key); - this.options.dispose(val, key, reason); + this.options.dispose(val!, key, reason); return true; } /** Clear the cache */ @@ -81,7 +81,7 @@ export class LRUCache { dump() { return [...this.cache]; } - load(entries: string | [string, LRUCacheItem][]) { + load(entries: string | [K, LRUCacheItem][]) { if (typeof entries == 'string') entries = JSON.parse(entries) as never; this.cache = new Map(entries); }