Skip to content

Commit

Permalink
perf(LRUCache): 改进 ts 类型声明的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Feb 2, 2024
1 parent 894b7f1 commit a376aa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 14 additions & 14 deletions src/common/lib/LRUCache.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// @see https://www.npmjs.com/package/lru-cache

export interface LRUCacheOptions {
export interface LRUCacheOptions<K = string, V = unknown> {
/** 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<V>, key: K, reason: string): void;
}

interface LRUCacheItem {
interface LRUCacheItem<V> {
t: number;
ttl: number;
v: unknown;
v: V;
}

export class LRUCache {
protected options: Required<LRUCacheOptions>;
protected cache = new Map<string, LRUCacheItem>();
constructor(options: LRUCacheOptions) {
export class LRUCache<K = string, V = unknown> {
protected options: Required<LRUCacheOptions<K, V>>;
protected cache = new Map<K, LRUCacheItem<V>>();
constructor(options: LRUCacheOptions<K, V>) {
options = {
ttl: 0,
dispose: () => void 0,
Expand All @@ -31,7 +31,7 @@ export class LRUCache {
this.options = options as never;
}
/** Add a value to the cache */
set<T>(key: string, value: T, opts?: { ttl?: number }) {
set<T extends V = V>(key: K, value: T, opts?: { ttl?: number }) {
const { cache } = this;

if (cache.has(key)) this.delete(key, 'set');
Expand All @@ -48,7 +48,7 @@ export class LRUCache {
}
}
/** Return a value from the cache */
get<T>(key: string, options: { updateAgeOnGet?: boolean } = {}) {
get<T extends V = V>(key: K, options: { updateAgeOnGet?: boolean } = {}) {
const cache = this.cache;
if (!cache.has(key)) return;
const value = cache.get(key)!;
Expand All @@ -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 */
Expand All @@ -81,7 +81,7 @@ export class LRUCache {
dump() {
return [...this.cache];
}
load(entries: string | [string, LRUCacheItem][]) {
load(entries: string | [K, LRUCacheItem<V>][]) {
if (typeof entries == 'string') entries = JSON.parse(entries) as never;
this.cache = new Map(entries);
}
Expand Down

0 comments on commit a376aa9

Please sign in to comment.