-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(performance): getRectByTaro 方法在小程序内增加缓存以提升性能 (#2831)
* feat: getRectByTaro 方法在小程序内增加缓存以提升性能 * feat: lock 文件提交 * feat: 工具类新增 lru * feat: 增加capacity 参数值校验 * feat: lock 文件还原
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export default class MiniLru { | ||
private cache: Map<any, any> | ||
|
||
private capacity: number | ||
|
||
constructor(capacity: number) { | ||
if (capacity <= 0) { | ||
throw new Error('Cache capacity must be a positive number') | ||
} | ||
this.cache = new Map() | ||
this.capacity = capacity | ||
} | ||
|
||
get(key: any): any | null { | ||
if (this.cache.has(key)) { | ||
const value = this.cache.get(key) | ||
this.cache.delete(key) | ||
this.cache.set(key, value) | ||
return value | ||
} | ||
return null | ||
} | ||
|
||
set(key: any, value: any): void { | ||
if (this.cache.has(key)) { | ||
this.cache.delete(key) | ||
} else if (this.cache.size >= this.capacity) { | ||
this.cache.delete(this.cache.keys().next().value) | ||
} | ||
this.cache.set(key, value) | ||
} | ||
|
||
has(key: any): boolean { | ||
return this.cache.has(key) | ||
} | ||
} |