Skip to content

Commit 5b524b7

Browse files
author
Simon he
committed
chore: add getLru
1 parent ea2987c commit 5b524b7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/getLru.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { LRU } from './types'
2+
export function getLru(max = 50): LRU {
3+
return {
4+
set(key: string, value: any) {
5+
if (this.cache.has(key))
6+
this.cache.delete(key)
7+
this.cache.set(key, value)
8+
if (this.cache.size > this.max)
9+
this.cache.delete(this.cache.keys().next().value)
10+
},
11+
get(key: string) {
12+
if (this.cache.has(key)) {
13+
const value = this.cache.get(key)
14+
this.cache.delete(key)
15+
this.cache.set(key, value)
16+
return value
17+
}
18+
return undefined
19+
},
20+
cache: new Map(),
21+
max,
22+
}
23+
}

0 commit comments

Comments
 (0)