mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf(plugin-shikiji): 优化开发时编译速度
This commit is contained in:
parent
4adc7862b5
commit
4d950b7618
39
plugins/plugin-shikiji/src/node/lru.ts
Normal file
39
plugins/plugin-shikiji/src/node/lru.ts
Normal file
@ -0,0 +1,39 @@
|
||||
// adapted from https://stackoverflow.com/a/46432113/11613622
|
||||
|
||||
export class LRUCache<K, V> {
|
||||
private max: number
|
||||
private cache: Map<K, V>
|
||||
|
||||
constructor(max: number = 10) {
|
||||
this.max = max
|
||||
this.cache = new Map<K, V>()
|
||||
}
|
||||
|
||||
get(key: K): V | undefined {
|
||||
const item = this.cache.get(key)
|
||||
if (item !== undefined) {
|
||||
// refresh key
|
||||
this.cache.delete(key)
|
||||
this.cache.set(key, item)
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
set(key: K, val: V): void {
|
||||
// refresh key
|
||||
if (this.cache.has(key))
|
||||
this.cache.delete(key)
|
||||
// evict oldest
|
||||
else if (this.cache.size === this.max)
|
||||
this.cache.delete(this.first()!)
|
||||
this.cache.set(key, val)
|
||||
}
|
||||
|
||||
first(): K | undefined {
|
||||
return this.cache.keys().next().value
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.cache.clear()
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user