@chriscdn/memoize 1.0.5 → 1.0.7
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.
- package/LICENSE +1 -1
- package/README.md +35 -18
- package/__tests__/memoize.test.ts +59 -0
- package/lib/index.d.ts +4 -3
- package/lib/memoize.cjs +1 -1
- package/lib/memoize.cjs.map +1 -1
- package/lib/memoize.modern.js +1 -1
- package/lib/memoize.modern.js.map +1 -1
- package/lib/memoize.module.js +1 -1
- package/lib/memoize.module.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +14 -5
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -18,21 +18,22 @@ yarn add @chriscdn/memoize
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
The package
|
|
21
|
+
The package provides two functions: `Memoize` and `MemoizeAsync`.
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
import { Memoize, MemoizeAsync } from "@chriscdn/memoize";
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
- **`Memoize`** is used to memoize a _synchronous_ function.
|
|
28
|
+
- **`MemoizeAsync`** is used to memoize an _asynchronous_ function.
|
|
28
29
|
|
|
29
|
-
Each call to `Memoize` or `MemoizeAsync`
|
|
30
|
+
The cache is powered by [quick-lru](https://www.npmjs.com/package/quick-lru). Each call to `Memoize` or `MemoizeAsync` creates a new cache instance.
|
|
30
31
|
|
|
31
|
-
The `MemoizeAsync` function prevents duplicate evaluations by ensuring multiple calls with
|
|
32
|
+
The `MemoizeAsync` function prevents duplicate evaluations by ensuring that multiple calls with the same cache key are processed only once.
|
|
32
33
|
|
|
33
|
-
By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This can be customized (see below).
|
|
34
|
+
By default, the cache key is generated by calling `JSON.stringify()` on the function arguments. This behavior can be customized (see below).
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
### Example (Synchronous)
|
|
36
37
|
|
|
37
38
|
To memoize a function:
|
|
38
39
|
|
|
@@ -49,9 +50,15 @@ const result = add(5, 7);
|
|
|
49
50
|
// 12
|
|
50
51
|
```
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
You can also define the function in a single line:
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
```ts
|
|
56
|
+
const add = Memoize((x: number, y: number) => x + y);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Example (Asynchronous)
|
|
60
|
+
|
|
61
|
+
Memoizing an asynchronous function is similar:
|
|
55
62
|
|
|
56
63
|
```ts
|
|
57
64
|
const _add = async (x: number, y: number) => x + y;
|
|
@@ -64,44 +71,52 @@ const result = await add(5, 7);
|
|
|
64
71
|
|
|
65
72
|
## Options
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
Both `Memoize` and `MemoizeAsync` accept an `options` parameter to control the cache behavior:
|
|
68
75
|
|
|
69
76
|
```ts
|
|
70
77
|
const add = MemoizeAsync(_add, options);
|
|
71
78
|
```
|
|
72
79
|
|
|
73
|
-
|
|
80
|
+
Available options (with defaults):
|
|
74
81
|
|
|
75
82
|
```ts
|
|
76
83
|
const options = {
|
|
77
|
-
//
|
|
84
|
+
// Maximum number of items in the cache
|
|
78
85
|
maxSize: 1000,
|
|
79
86
|
|
|
80
|
-
//
|
|
87
|
+
// Maximum lifespan of an item in milliseconds; undefined means items never expire
|
|
81
88
|
maxAge: undefined,
|
|
82
89
|
|
|
83
|
-
//
|
|
90
|
+
// A synchronous function whether to add the returnValue to the cache.
|
|
91
|
+
shouldCache: (returnValue: Return, key: string) => true,
|
|
92
|
+
|
|
93
|
+
// A synchronous function to generate cache keys (must return a string)
|
|
84
94
|
resolver: (...args) => JSON.stringify(args),
|
|
85
95
|
};
|
|
86
96
|
```
|
|
87
97
|
|
|
88
98
|
## Cache
|
|
89
99
|
|
|
90
|
-
The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance
|
|
100
|
+
The underlying [quick-lru](https://www.npmjs.com/package/quick-lru) instance is accessible via the `.cache` property on the memoized function:
|
|
91
101
|
|
|
92
102
|
```ts
|
|
93
103
|
const add = MemoizeAsync(_add);
|
|
94
104
|
const result = await add(5, 7);
|
|
95
105
|
|
|
96
|
-
console.log(
|
|
106
|
+
console.log(add.cache.size === 1);
|
|
97
107
|
// true
|
|
108
|
+
|
|
109
|
+
// Clear the cache
|
|
110
|
+
add.cache.clear();
|
|
98
111
|
```
|
|
99
112
|
|
|
113
|
+
The values `null` and `undefined` are cached by default, but this behavior can be adjusted using the `shouldCache` option.
|
|
114
|
+
|
|
100
115
|
## Class Methods
|
|
101
116
|
|
|
102
|
-
Class methods can also be memoized,
|
|
117
|
+
Class methods can also be memoized, but this requires overriding the method within the constructor. Ensure you bind the method to the instance to maintain the correct context.
|
|
103
118
|
|
|
104
|
-
|
|
119
|
+
Here's an example where the `add` method is memoized by reassigning it within the constructor. This approach preserves access to instance properties (like `count`) and maintains the correct method signature in TypeScript:
|
|
105
120
|
|
|
106
121
|
```ts
|
|
107
122
|
class AddClass {
|
|
@@ -118,10 +133,12 @@ class AddClass {
|
|
|
118
133
|
}
|
|
119
134
|
```
|
|
120
135
|
|
|
121
|
-
|
|
136
|
+
Each memoized method in each class instance maintains its own cache.
|
|
122
137
|
|
|
123
138
|
## Tests
|
|
124
139
|
|
|
140
|
+
Run the tests using:
|
|
141
|
+
|
|
125
142
|
```bash
|
|
126
143
|
yarn test
|
|
127
144
|
```
|
|
@@ -102,3 +102,62 @@ describe("Memoization of Class methods", () => {
|
|
|
102
102
|
expect(obj.count).toBe(2);
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
|
+
|
|
106
|
+
describe("Null & Undefined Cases", () => {
|
|
107
|
+
const UndefinedFunc = Memoize((key: string) => undefined, {
|
|
108
|
+
resolver: (key) => key,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const NullFunc = Memoize((key: string) => undefined, {
|
|
112
|
+
resolver: (key) => key,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("Undefined", () => {
|
|
116
|
+
expect(UndefinedFunc.cache.has("hello")).toBe(false);
|
|
117
|
+
expect(UndefinedFunc("hello")).toBe(undefined);
|
|
118
|
+
expect(UndefinedFunc.cache.has("hello")).toBe(true);
|
|
119
|
+
expect(UndefinedFunc("hello")).toBe(undefined);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("Null", () => {
|
|
123
|
+
expect(NullFunc.cache.has("hello")).toBe(false);
|
|
124
|
+
expect(NullFunc("hello")).toBe(undefined);
|
|
125
|
+
expect(NullFunc.cache.has("hello")).toBe(true);
|
|
126
|
+
expect(NullFunc("hello")).toBe(undefined);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("Object Reference", () => {
|
|
131
|
+
const a = { hello: "world" };
|
|
132
|
+
|
|
133
|
+
const funny = Memoize(() => a);
|
|
134
|
+
|
|
135
|
+
it("Null", () => {
|
|
136
|
+
expect(funny().hello).toBe("world");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("Null", () => {
|
|
140
|
+
a.hello = "mars";
|
|
141
|
+
expect(funny().hello).toBe("mars");
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("ShouldCache", () => {
|
|
146
|
+
const doNotCache = "do not cache";
|
|
147
|
+
|
|
148
|
+
const myFunction = Memoize((word: string) => word, {
|
|
149
|
+
shouldCache: (value) => value !== doNotCache,
|
|
150
|
+
resolver: (value) => value,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
myFunction("hi");
|
|
154
|
+
myFunction(doNotCache);
|
|
155
|
+
|
|
156
|
+
it("should be cached", () => {
|
|
157
|
+
expect(myFunction.cache.has("hi")).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("should not be cached", () => {
|
|
161
|
+
expect(myFunction.cache.has(doNotCache)).toBe(false);
|
|
162
|
+
});
|
|
163
|
+
});
|
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import QuickLRU from "quick-lru";
|
|
2
|
-
type Options<T extends any[]> = {
|
|
2
|
+
type Options<T extends any[], Return> = {
|
|
3
3
|
maxSize: number;
|
|
4
4
|
maxAge?: number;
|
|
5
|
+
shouldCache: (returnValue: Return, key: string) => boolean;
|
|
5
6
|
resolver: (...args: T) => string;
|
|
6
7
|
};
|
|
7
8
|
/**
|
|
8
9
|
* Memoize a synchronous function.
|
|
9
10
|
*/
|
|
10
|
-
declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => Return, options?: Partial<Options<Args>>) => {
|
|
11
|
+
declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => Return, options?: Partial<Options<Args, Return>>) => {
|
|
11
12
|
(...args: Args): Return;
|
|
12
13
|
cache: QuickLRU<string, Return>;
|
|
13
14
|
};
|
|
@@ -19,7 +20,7 @@ declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => R
|
|
|
19
20
|
* semaphore, which forces redundant calls to wait until the first call
|
|
20
21
|
* completes.
|
|
21
22
|
*/
|
|
22
|
-
declare const MemoizeAsync: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<Options<Args>>) => {
|
|
23
|
+
declare const MemoizeAsync: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<Options<Args, Return>>) => {
|
|
23
24
|
(...args: Args): Promise<Return>;
|
|
24
25
|
cache: QuickLRU<string, Return>;
|
|
25
26
|
};
|
package/lib/memoize.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@chriscdn/promise-semaphore"),r=require("quick-lru");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/n(e),
|
|
1
|
+
var e=require("@chriscdn/promise-semaphore"),r=require("quick-lru");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/n(e),u=/*#__PURE__*/n(r);exports.Memoize=function(e,r){var n,t,i;void 0===r&&(r={});var l=null!=(n=r.maxSize)?n:1e3,a=null!=(t=r.shouldCache)?t:function(){return!0},o=null!=(i=r.resolver)?i:function(){return JSON.stringify([].slice.call(arguments))},c=new u.default({maxAge:r.maxAge,maxSize:l}),s=function(){var r=[].slice.call(arguments),n=o.apply(void 0,r);if(c.has(n))return c.get(n);var t=e.apply(void 0,r);return a(t,n)&&c.set(n,t),t};return s.cache=c,s},exports.MemoizeAsync=function(e,r){var n,i,l;void 0===r&&(r={});var a=r.maxAge,o=null!=(n=r.maxSize)?n:1e3,c=null!=(i=r.shouldCache)?i:function(){return!0},s=null!=(l=r.resolver)?l:function(){return JSON.stringify([].slice.call(arguments))},f=new t.default,v=new u.default({maxAge:a,maxSize:o}),h=function(){try{var r=[].slice.call(arguments),n=s.apply(void 0,r);return Promise.resolve(v.has(n)?v.get(n):function(t,u){try{var i=Promise.resolve(f.acquire(n)).then(function(){return v.has(n)?v.get(n):Promise.resolve(e.apply(void 0,r)).then(function(e){return c(e,n)&&v.set(n,e),e})})}catch(e){return u(!0,e)}return i&&i.then?i.then(u.bind(null,!1),u.bind(null,!0)):u(!1,i)}(0,function(e,r){if(f.release(n),e)throw r;return r}))}catch(e){return Promise.reject(e)}};return h.cache=v,h};
|
|
2
2
|
//# sourceMappingURL=memoize.cjs.map
|
package/lib/memoize.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.cjs","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[]> = {\n maxSize: number;\n maxAge?: number;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["cb","options","_options$maxSize","_options$resolver","maxSize","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","_options$maxSize2","_options$resolver2","semaphore","Semaphore","
|
|
1
|
+
{"version":3,"file":"memoize.cjs","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","Promise","resolve","acquire","then","_finallyRethrows","_wasThrown","_result","release","e","reject"],"mappings":"wMAegB,SACdA,EACAC,GACE,IAAAC,EAAAC,EAAAC,OADFH,IAAAA,IAAAA,EAA0C,CAAA,GAE1C,IACMI,EAAyBH,OAAlBA,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiCH,OAAtBA,EAAGF,EAAQK,aAAWH,EAAK,WAAM,OAAA,CAAI,EAEhDI,EACYH,OADJA,EACZH,EAAQM,UAAQH,EAAK,WAAA,OAAmBI,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAExDC,EAAQ,IAAIC,UAAyB,CACzCC,OARiCd,EAAQc,OASzCV,QAAAA,IAGIW,EAAmB,WAAI,IAAAC,EAAU,GAAAP,MAAAC,KAAAC,WAC/BM,EAAMX,EAAQY,WAAA,EAAIF,GAExB,GAAIJ,EAAMO,IAAIF,GACZ,OAAOL,EAAMQ,IAAIH,GAEjB,IAAMI,EAActB,EAAEmB,WAAIF,EAAAA,GAI1B,OAHIX,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAEVA,CAEX,EAIA,OAFAN,EAAiBH,MAAQA,EAElBG,CACT,uBAUqB,SACnBhB,EACAC,GACEuB,IAAAA,EAAAC,EAAAC,OADwC,IAA1CzB,IAAAA,EAA0C,IAE1C,IAAMc,EAA6Bd,EAAQc,OACrCV,EAAyBmB,OAAlBA,EAAGvB,EAAQI,SAAOmB,EA5DT,IA6DhBlB,EAAiCmB,OAAtBA,EAAGxB,EAAQK,aAAWmB,EAAK,WAAM,OAAA,CAAI,EAEhDlB,SAAQmB,EACZzB,EAAQM,UAAQmB,EAAK,WAAmB,OAAAlB,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAExDe,EAAY,IAAIC,EAAW,QAE3Bf,EAAQ,IAAIC,EAAAA,QAAyB,CACzCC,OAAAA,EACAV,QAAAA,IAGIW,EAAA,WAA4D,IAAA,IAA/BC,EAAUP,GAAAA,MAAAC,KAAqBC,WAC1DM,EAAMX,EAAQY,WAAIF,EAAAA,GAAM,OAAAY,QAAAC,QAE1BjB,EAAMO,IAAIF,GACLL,EAAMQ,IAAIH,2BAEbW,QAAAC,QACIH,EAAUI,QAAQb,IAAIc,KAExBnB,WAAAA,OAAAA,EAAMO,IAAIF,GACLL,EAAMQ,IAAIH,GAAKW,QAAAC,QAEI9B,EAAEmB,WAAIF,EAAAA,IAAKe,KAAA,SAA/BV,GAMN,OAJIhB,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAGVA,CAAY,EAEtB,4FAhBsBW,YAgBtBC,EAAAC,GACwB,GAAvBR,EAAUS,QAAQlB,GAAKgB,QAAAC,EAAA,OAAAA,CAAA,GAG7B,CAAC,MAAAE,GAAA,OAAAR,QAAAS,OAAAD,EAEDrB,CAAAA,EAEA,OAFAA,EAAiBH,MAAQA,EAElBG,CACT"}
|
package/lib/memoize.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"@chriscdn/promise-semaphore";import r from"quick-lru";const
|
|
1
|
+
import e from"@chriscdn/promise-semaphore";import r from"quick-lru";const n=(e,n={})=>{var t,a,s;const i=null!=(t=n.maxSize)?t:1e3,l=null!=(a=n.shouldCache)?a:()=>!0,c=null!=(s=n.resolver)?s:(...e)=>JSON.stringify(e),o=new r({maxAge:n.maxAge,maxSize:i}),u=(...r)=>{const n=c(...r);if(o.has(n))return o.get(n);{const t=e(...r);return l(t,n)&&o.set(n,t),t}};return u.cache=o,u},t=(n,t={})=>{var a,s,i;const l=t.maxAge,c=null!=(a=t.maxSize)?a:1e3,o=null!=(s=t.shouldCache)?s:()=>!0,u=null!=(i=t.resolver)?i:(...e)=>JSON.stringify(e),m=new e,h=new r({maxAge:l,maxSize:c}),g=async(...e)=>{const r=u(...e);if(h.has(r))return h.get(r);try{if(await m.acquire(r),h.has(r))return h.get(r);{const t=await n(...e);return o(t,r)&&h.set(r,t),t}}finally{m.release(r)}};return g.cache=h,g};export{n as Memoize,t as MemoizeAsync};
|
|
2
2
|
//# sourceMappingURL=memoize.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.modern.js","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[]> = {\n maxSize: number;\n maxAge?: number;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$resolver","maxSize","resolver","args","JSON","stringify","cache","QuickLRU","maxAge","memoizedFunction","key","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$resolver2","semaphore","Semaphore","async","acquire","release"],"mappings":"oEAGA,
|
|
1
|
+
{"version":3,"file":"memoize.modern.js","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","args","JSON","stringify","cache","QuickLRU","maxAge","memoizedFunction","key","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","async","acquire","release"],"mappings":"oEAGA,MAYMA,EAAUA,CACdC,EACAC,EAA0C,CAAA,KACxCC,IAAAA,EAAAC,EAAAC,EACF,MACMC,EAAyBH,OAAlBA,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiC,OAAtBH,EAAGF,EAAQK,aAAWH,EAAK,KAAM,EAE5CI,SAAQH,EACZH,EAAQM,UAAQH,EAAK,IAAII,IAAeC,KAAKC,UAAUF,GAEnDG,EAAQ,IAAIC,EAAyB,CACzCC,OARiCZ,EAAQY,OASzCR,YAGIS,EAAmBA,IAAIN,KAC3B,MAAMO,EAAMR,KAAYC,GAExB,GAAIG,EAAMK,IAAID,GACZ,OAAOJ,EAAMM,IAAIF,GACZ,CACL,MAAMG,EAAclB,KAAMQ,GAI1B,OAHIF,EAAYY,EAAaH,IAC3BJ,EAAMQ,IAAIJ,EAAKG,GAEVA,CACR,GAKH,OAFAJ,EAAiBH,MAAQA,EAElBG,GAWHM,EAAeA,CACnBpB,EACAC,EAA0C,CAAE,SAC1CoB,EAAAC,EAAAC,EACF,MAAMV,EAA6BZ,EAAQY,OACrCR,EAAyB,OAAlBgB,EAAGpB,EAAQI,SAAOgB,EA5DT,IA6DhBf,EAAiCgB,OAAtBA,EAAGrB,EAAQK,aAAWgB,EAAK,KAAM,EAE5Cf,EACY,OADJgB,EACZtB,EAAQM,UAAQgB,EAAK,IAAIf,IAAeC,KAAKC,UAAUF,GAEnDgB,EAAY,IAAIC,EAEhBd,EAAQ,IAAIC,EAAyB,CACzCC,SACAR,YAGIS,EAAmBY,SAAUlB,KACjC,MAAMO,EAAMR,KAAYC,GAExB,GAAIG,EAAMK,IAAID,GACZ,OAAOJ,EAAMM,IAAIF,GAEjB,IAGE,SAFMS,EAAUG,QAAQZ,GAEpBJ,EAAMK,IAAID,GACZ,OAAOJ,EAAMM,IAAIF,GACZ,CACL,MAAMG,QAAoBlB,KAAMQ,GAMhC,OAJIF,EAAYY,EAAaH,IAC3BJ,EAAMQ,IAAIJ,EAAKG,GAGVA,CACR,CACF,CAAA,QACCM,EAAUI,QAAQb,EACnB,CACF,EAKH,OAFAD,EAAiBH,MAAQA,EAElBG"}
|
package/lib/memoize.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"@chriscdn/promise-semaphore";import r from"quick-lru";var n=function(e,n){var t,i;void 0===n&&(n={});var
|
|
1
|
+
import e from"@chriscdn/promise-semaphore";import r from"quick-lru";var n=function(e,n){var t,i,l;void 0===n&&(n={});var u=null!=(t=n.maxSize)?t:1e3,a=null!=(i=n.shouldCache)?i:function(){return!0},o=null!=(l=n.resolver)?l:function(){return JSON.stringify([].slice.call(arguments))},c=new r({maxAge:n.maxAge,maxSize:u}),s=function(){var r=[].slice.call(arguments),n=o.apply(void 0,r);if(c.has(n))return c.get(n);var t=e.apply(void 0,r);return a(t,n)&&c.set(n,t),t};return s.cache=c,s},t=function(n,t){var i,l,u;void 0===t&&(t={});var a=t.maxAge,o=null!=(i=t.maxSize)?i:1e3,c=null!=(l=t.shouldCache)?l:function(){return!0},s=null!=(u=t.resolver)?u:function(){return JSON.stringify([].slice.call(arguments))},v=new e,f=new r({maxAge:a,maxSize:o}),h=function(){try{var e=[].slice.call(arguments),r=s.apply(void 0,e);return Promise.resolve(f.has(r)?f.get(r):function(t,i){try{var l=Promise.resolve(v.acquire(r)).then(function(){return f.has(r)?f.get(r):Promise.resolve(n.apply(void 0,e)).then(function(e){return c(e,r)&&f.set(r,e),e})})}catch(e){return i(!0,e)}return l&&l.then?l.then(i.bind(null,!1),i.bind(null,!0)):i(!1,l)}(0,function(e,n){if(v.release(r),e)throw n;return n}))}catch(e){return Promise.reject(e)}};return h.cache=f,h};export{n as Memoize,t as MemoizeAsync};
|
|
2
2
|
//# sourceMappingURL=memoize.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.module.js","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[]> = {\n maxSize: number;\n maxAge?: number;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n cache.set(key, returnValue);\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$resolver","maxSize","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$resolver2","semaphore","Semaphore","
|
|
1
|
+
{"version":3,"file":"memoize.module.js","sources":["../src/index.ts"],"sourcesContent":["import Semaphore from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n *\n * This differs from the synchronous case by ensuring multiple calls with the\n * same arguments is only evaluated once. This is controlled by using a\n * semaphore, which forces redundant calls to wait until the first call\n * completes.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver =\n options.resolver ?? ((...args: Args) => JSON.stringify(args));\n\n const semaphore = new Semaphore();\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key)!;\n } else {\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key);\n } else {\n const returnValue = await cb(...args);\n\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","Promise","resolve","acquire","then","_finallyRethrows","_wasThrown","_result","release","e","reject"],"mappings":"oEAGA,IAYMA,EAAU,SACdC,EACAC,GACE,IAAAC,EAAAC,EAAAC,OADFH,IAAAA,IAAAA,EAA0C,CAAA,GAE1C,IACMI,EAAyBH,OAAlBA,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiCH,OAAtBA,EAAGF,EAAQK,aAAWH,EAAK,WAAM,OAAA,CAAI,EAEhDI,EACYH,OADJA,EACZH,EAAQM,UAAQH,EAAK,WAAA,OAAmBI,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAExDC,EAAQ,IAAIC,EAAyB,CACzCC,OARiCd,EAAQc,OASzCV,QAAAA,IAGIW,EAAmB,WAAI,IAAAC,EAAU,GAAAP,MAAAC,KAAAC,WAC/BM,EAAMX,EAAQY,WAAA,EAAIF,GAExB,GAAIJ,EAAMO,IAAIF,GACZ,OAAOL,EAAMQ,IAAIH,GAEjB,IAAMI,EAActB,EAAEmB,WAAIF,EAAAA,GAI1B,OAHIX,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAEVA,CAEX,EAIA,OAFAN,EAAiBH,MAAQA,EAElBG,CACT,EAUMQ,EAAe,SACnBxB,EACAC,GACEwB,IAAAA,EAAAC,EAAAC,OADwC,IAA1C1B,IAAAA,EAA0C,IAE1C,IAAMc,EAA6Bd,EAAQc,OACrCV,EAAyBoB,OAAlBA,EAAGxB,EAAQI,SAAOoB,EA5DT,IA6DhBnB,EAAiCoB,OAAtBA,EAAGzB,EAAQK,aAAWoB,EAAK,WAAM,OAAA,CAAI,EAEhDnB,SAAQoB,EACZ1B,EAAQM,UAAQoB,EAAK,WAAmB,OAAAnB,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAExDgB,EAAY,IAAIC,EAEhBhB,EAAQ,IAAIC,EAAyB,CACzCC,OAAAA,EACAV,QAAAA,IAGIW,EAAA,WAA4D,IAAA,IAA/BC,EAAUP,GAAAA,MAAAC,KAAqBC,WAC1DM,EAAMX,EAAQY,WAAIF,EAAAA,GAAM,OAAAa,QAAAC,QAE1BlB,EAAMO,IAAIF,GACLL,EAAMQ,IAAIH,2BAEbY,QAAAC,QACIH,EAAUI,QAAQd,IAAIe,KAExBpB,WAAAA,OAAAA,EAAMO,IAAIF,GACLL,EAAMQ,IAAIH,GAAKY,QAAAC,QAEI/B,EAAEmB,WAAIF,EAAAA,IAAKgB,KAAA,SAA/BX,GAMN,OAJIhB,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAGVA,CAAY,EAEtB,4FAhBsBY,YAgBtBC,EAAAC,GACwB,GAAvBR,EAAUS,QAAQnB,GAAKiB,QAAAC,EAAA,OAAAA,CAAA,GAG7B,CAAC,MAAAE,GAAA,OAAAR,QAAAS,OAAAD,EAEDtB,CAAAA,EAEA,OAFAA,EAAiBH,MAAQA,EAElBG,CACT"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,9 +3,10 @@ import QuickLRU from "quick-lru";
|
|
|
3
3
|
|
|
4
4
|
const kDefaultMaxSize = 1000;
|
|
5
5
|
|
|
6
|
-
type Options<T extends any[]> = {
|
|
6
|
+
type Options<T extends any[], Return> = {
|
|
7
7
|
maxSize: number;
|
|
8
8
|
maxAge?: number;
|
|
9
|
+
shouldCache: (returnValue: Return, key: string) => boolean;
|
|
9
10
|
resolver: (...args: T) => string;
|
|
10
11
|
};
|
|
11
12
|
|
|
@@ -14,10 +15,11 @@ type Options<T extends any[]> = {
|
|
|
14
15
|
*/
|
|
15
16
|
const Memoize = <Args extends unknown[], Return>(
|
|
16
17
|
cb: (...args: Args) => Return,
|
|
17
|
-
options: Partial<Options<Args>> = {},
|
|
18
|
+
options: Partial<Options<Args, Return>> = {},
|
|
18
19
|
) => {
|
|
19
20
|
const maxAge: number | undefined = options.maxAge;
|
|
20
21
|
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
22
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
21
23
|
|
|
22
24
|
const resolver =
|
|
23
25
|
options.resolver ?? ((...args: Args) => JSON.stringify(args));
|
|
@@ -34,7 +36,9 @@ const Memoize = <Args extends unknown[], Return>(
|
|
|
34
36
|
return cache.get(key);
|
|
35
37
|
} else {
|
|
36
38
|
const returnValue = cb(...args);
|
|
37
|
-
|
|
39
|
+
if (shouldCache(returnValue, key)) {
|
|
40
|
+
cache.set(key, returnValue);
|
|
41
|
+
}
|
|
38
42
|
return returnValue;
|
|
39
43
|
}
|
|
40
44
|
};
|
|
@@ -54,10 +58,11 @@ const Memoize = <Args extends unknown[], Return>(
|
|
|
54
58
|
*/
|
|
55
59
|
const MemoizeAsync = <Args extends unknown[], Return>(
|
|
56
60
|
cb: (...args: Args) => Promise<Return>,
|
|
57
|
-
options: Partial<Options<Args>> = {},
|
|
61
|
+
options: Partial<Options<Args, Return>> = {},
|
|
58
62
|
) => {
|
|
59
63
|
const maxAge: number | undefined = options.maxAge;
|
|
60
64
|
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
65
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
61
66
|
|
|
62
67
|
const resolver =
|
|
63
68
|
options.resolver ?? ((...args: Args) => JSON.stringify(args));
|
|
@@ -82,7 +87,11 @@ const MemoizeAsync = <Args extends unknown[], Return>(
|
|
|
82
87
|
return cache.get(key);
|
|
83
88
|
} else {
|
|
84
89
|
const returnValue = await cb(...args);
|
|
85
|
-
|
|
90
|
+
|
|
91
|
+
if (shouldCache(returnValue, key)) {
|
|
92
|
+
cache.set(key, returnValue);
|
|
93
|
+
}
|
|
94
|
+
|
|
86
95
|
return returnValue;
|
|
87
96
|
}
|
|
88
97
|
} finally {
|