@naturalcycles/nodejs-lib 15.128.0 → 15.129.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MemoCache } from '@naturalcycles/js-lib/decorators';
|
|
1
|
+
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators';
|
|
2
2
|
import { LRUCache } from 'lru-cache';
|
|
3
3
|
export type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE, any>>;
|
|
4
4
|
/**
|
|
@@ -16,3 +16,14 @@ export declare class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<K
|
|
|
16
16
|
set(k: any, v: any): void;
|
|
17
17
|
clear(): void;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
21
|
+
*/
|
|
22
|
+
export declare class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
23
|
+
constructor(opt: LRUMemoCacheOptions<KEY, VALUE>);
|
|
24
|
+
private lru;
|
|
25
|
+
has(k: any): Promise<boolean>;
|
|
26
|
+
get(k: any): Promise<any>;
|
|
27
|
+
set(k: any, v: any): Promise<void>;
|
|
28
|
+
clear(): Promise<void>;
|
|
29
|
+
}
|
|
@@ -27,3 +27,27 @@ export class LRUMemoCache {
|
|
|
27
27
|
this.lru.clear();
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
32
|
+
*/
|
|
33
|
+
export class LRUAsyncMemoCache {
|
|
34
|
+
constructor(opt) {
|
|
35
|
+
this.lru = new LRUCache({
|
|
36
|
+
max: 100,
|
|
37
|
+
...opt,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
lru;
|
|
41
|
+
async has(k) {
|
|
42
|
+
return this.lru.has(k);
|
|
43
|
+
}
|
|
44
|
+
async get(k) {
|
|
45
|
+
return this.lru.get(k);
|
|
46
|
+
}
|
|
47
|
+
async set(k, v) {
|
|
48
|
+
this.lru.set(k, v);
|
|
49
|
+
}
|
|
50
|
+
async clear() {
|
|
51
|
+
this.lru.clear();
|
|
52
|
+
}
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MemoCache } from '@naturalcycles/js-lib/decorators'
|
|
1
|
+
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators'
|
|
2
2
|
import { LRUCache } from 'lru-cache'
|
|
3
3
|
|
|
4
4
|
// Partial, to be able to provide default `max`
|
|
@@ -37,3 +37,33 @@ export class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALU
|
|
|
37
37
|
this.lru.clear()
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
43
|
+
*/
|
|
44
|
+
export class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
45
|
+
constructor(opt: LRUMemoCacheOptions<KEY, VALUE>) {
|
|
46
|
+
this.lru = new LRUCache<any, any>({
|
|
47
|
+
max: 100,
|
|
48
|
+
...opt,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private lru!: LRUCache<any, any>
|
|
53
|
+
|
|
54
|
+
async has(k: any): Promise<boolean> {
|
|
55
|
+
return this.lru.has(k)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async get(k: any): Promise<any> {
|
|
59
|
+
return this.lru.get(k)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async set(k: any, v: any): Promise<void> {
|
|
63
|
+
this.lru.set(k, v)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async clear(): Promise<void> {
|
|
67
|
+
this.lru.clear()
|
|
68
|
+
}
|
|
69
|
+
}
|