@naturalcycles/nodejs-lib 15.128.0 → 15.129.1
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,5 @@
|
|
|
1
|
-
import type { MemoCache } from '@naturalcycles/js-lib/decorators';
|
|
1
|
+
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators';
|
|
2
|
+
import { MISS } from '@naturalcycles/js-lib/types';
|
|
2
3
|
import { LRUCache } from 'lru-cache';
|
|
3
4
|
export type LRUMemoCacheOptions<KEY, VALUE> = Partial<LRUCache.Options<KEY, VALUE, any>>;
|
|
4
5
|
/**
|
|
@@ -16,3 +17,14 @@ export declare class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<K
|
|
|
16
17
|
set(k: any, v: any): void;
|
|
17
18
|
clear(): void;
|
|
18
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
22
|
+
*/
|
|
23
|
+
export declare class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
24
|
+
constructor(opt: LRUMemoCacheOptions<KEY, VALUE>);
|
|
25
|
+
private lru;
|
|
26
|
+
has(k: any): Promise<boolean>;
|
|
27
|
+
get(k: any): Promise<VALUE | typeof MISS>;
|
|
28
|
+
set(k: any, v: any): Promise<void>;
|
|
29
|
+
clear(): Promise<void>;
|
|
30
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MISS } from '@naturalcycles/js-lib/types';
|
|
1
2
|
import { LRUCache } from 'lru-cache';
|
|
2
3
|
/**
|
|
3
4
|
* @example
|
|
@@ -27,3 +28,32 @@ export class LRUMemoCache {
|
|
|
27
28
|
this.lru.clear();
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
// lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
|
|
32
|
+
const UNDEFINED = Symbol('undefined');
|
|
33
|
+
/**
|
|
34
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
35
|
+
*/
|
|
36
|
+
export class LRUAsyncMemoCache {
|
|
37
|
+
constructor(opt) {
|
|
38
|
+
this.lru = new LRUCache({
|
|
39
|
+
max: 100,
|
|
40
|
+
...opt,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
lru;
|
|
44
|
+
async has(k) {
|
|
45
|
+
return this.lru.has(k);
|
|
46
|
+
}
|
|
47
|
+
async get(k) {
|
|
48
|
+
const v = this.lru.get(k);
|
|
49
|
+
if (v === undefined)
|
|
50
|
+
return MISS;
|
|
51
|
+
return v === UNDEFINED ? undefined : v;
|
|
52
|
+
}
|
|
53
|
+
async set(k, v) {
|
|
54
|
+
this.lru.set(k, v === undefined ? UNDEFINED : v);
|
|
55
|
+
}
|
|
56
|
+
async clear() {
|
|
57
|
+
this.lru.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { MemoCache } from '@naturalcycles/js-lib/decorators'
|
|
1
|
+
import type { AsyncMemoCache, MemoCache } from '@naturalcycles/js-lib/decorators'
|
|
2
|
+
import { MISS } from '@naturalcycles/js-lib/types'
|
|
2
3
|
import { LRUCache } from 'lru-cache'
|
|
3
4
|
|
|
4
5
|
// Partial, to be able to provide default `max`
|
|
@@ -37,3 +38,38 @@ export class LRUMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALU
|
|
|
37
38
|
this.lru.clear()
|
|
38
39
|
}
|
|
39
40
|
}
|
|
41
|
+
|
|
42
|
+
// lru-cache never stores `undefined` (`set(k, undefined)` deletes `k`), so it's stored as this sentinel
|
|
43
|
+
const UNDEFINED = Symbol('undefined')
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Same as LRUMemoCache, but implements AsyncMemoCache interface, for when you need it.
|
|
47
|
+
*/
|
|
48
|
+
export class LRUAsyncMemoCache<KEY = any, VALUE = any> implements AsyncMemoCache<KEY, VALUE> {
|
|
49
|
+
constructor(opt: LRUMemoCacheOptions<KEY, VALUE>) {
|
|
50
|
+
this.lru = new LRUCache<any, any>({
|
|
51
|
+
max: 100,
|
|
52
|
+
...opt,
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private lru!: LRUCache<any, any>
|
|
57
|
+
|
|
58
|
+
async has(k: any): Promise<boolean> {
|
|
59
|
+
return this.lru.has(k)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async get(k: any): Promise<VALUE | typeof MISS> {
|
|
63
|
+
const v = this.lru.get(k)
|
|
64
|
+
if (v === undefined) return MISS
|
|
65
|
+
return v === UNDEFINED ? (undefined as VALUE) : v
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async set(k: any, v: any): Promise<void> {
|
|
69
|
+
this.lru.set(k, v === undefined ? UNDEFINED : v)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async clear(): Promise<void> {
|
|
73
|
+
this.lru.clear()
|
|
74
|
+
}
|
|
75
|
+
}
|