UNPKG

660 BPlain TextView Raw
1import { MemoCache } from '@naturalcycles/js-lib'
2import LRUCache = require('lru-cache')
3
4export type LRUMemoCacheOpts = LRUCache.Options<string, any>
5
6/**
7 * @example
8 * Use it like this:
9 *
10 * @_Memo({ cacheFactory: () => new LRUMemoCache({...}) })
11 * method1 ()
12 */
13export class LRUMemoCache implements MemoCache {
14 constructor(opt: LRUMemoCacheOpts) {
15 this.lru = new LRUCache<string, any>(opt)
16 }
17
18 private lru!: LRUCache<string, any>
19
20 has(k: any): boolean {
21 return this.lru.has(k)
22 }
23
24 get(k: any): any {
25 return this.lru.get(k)
26 }
27
28 set(k: any, v: any): void {
29 this.lru.set(k, v)
30 }
31
32 clear(): void {
33 this.lru.reset()
34 }
35}