cache-manager 6.0.0 → 6.1.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.
- package/README.md +14 -6
- package/dist/index.cjs +42 -6
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +42 -6
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,16 +12,16 @@ A cache module for NodeJS that allows easy wrapping of functions in cache, tiere
|
|
|
12
12
|
- Made with Typescript and compatible with [ESModules](https://nodejs.org/docs/latest-v14.x/api/esm.html).
|
|
13
13
|
- Easy way to wrap any function in cache, supports a mechanism to refresh expiring cache keys in background.
|
|
14
14
|
- Tiered caches -- data gets stored in each cache and fetched from the highest priority cache(s) first.
|
|
15
|
-
- Use with any [Keyv](https://keyv.org/)
|
|
15
|
+
- Use with any [Keyv](https://keyv.org/) compatible storage adapter.
|
|
16
16
|
- 100% test coverage via [vitest](https://github.com/vitest-dev/vitest).
|
|
17
17
|
|
|
18
|
-
We moved to using [Keyv](https://keyv.org/)
|
|
18
|
+
We moved to using [Keyv](https://keyv.org/) which are more actively maintained and have a larger community.
|
|
19
19
|
|
|
20
|
-
A special thanks to [Tim Phan](https://github.com/timphandev) who tooke `cache-manager` v5 and ported it to [Keyv](https://keyv.org/) which is the foundation of v6. 🎉 Another special thanks to
|
|
20
|
+
A special thanks to [Tim Phan](https://github.com/timphandev) who tooke `cache-manager` v5 and ported it to [Keyv](https://keyv.org/) which is the foundation of v6. 🎉 Another special thanks to [Doug Ayers](https://github.com/douglascayers) who wrote `promise-coalesce` which was used in v5 and now embedded in v6.
|
|
21
21
|
|
|
22
22
|
If you are looking for older documentation you can find it here:
|
|
23
|
-
* [v5](
|
|
24
|
-
* [v4](
|
|
23
|
+
* [v5 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md)
|
|
24
|
+
* [v4 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv4.md)
|
|
25
25
|
|
|
26
26
|
## Table of Contents
|
|
27
27
|
* [Installation](#installation)
|
|
@@ -165,6 +165,14 @@ const cache = createCache({ stores: [keyv] });
|
|
|
165
165
|
|
|
166
166
|
If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.
|
|
167
167
|
|
|
168
|
+
- **nonBlocking**?: boolean - Default false
|
|
169
|
+
|
|
170
|
+
If set to true, the system will not block when multiple stores are used. Here is how it affects tye type of functions:
|
|
171
|
+
* `set and mset` - will not wait for all stores to finish.
|
|
172
|
+
* `get and mget` - will return the first (fastest) value found.
|
|
173
|
+
* `del and mdel` - will not wait for all stores to finish.
|
|
174
|
+
* `clear` - will not wait for all stores to finish.
|
|
175
|
+
|
|
168
176
|
## Methods
|
|
169
177
|
### set
|
|
170
178
|
`set(key, value, [ttl]): Promise<value>`
|
|
@@ -365,7 +373,7 @@ See unit tests in [`test/events.test.ts`](./test/events.test.ts) for more inform
|
|
|
365
373
|
|
|
366
374
|
## Update on `redis` and `ioredis` Support
|
|
367
375
|
|
|
368
|
-
We will not be supporting `cache-manager-ioredis-yet` or `cache-manager-redis-yet` in the future as we have moved to using `Keyv` as the storage adapter `@keyv/redis`.
|
|
376
|
+
We will not be supporting `cache-manager-ioredis-yet` or `cache-manager-redis-yet` in the future as we have moved to using `Keyv` as the storage adapter `@keyv/redis`.
|
|
369
377
|
|
|
370
378
|
## Using Legacy Storage Adapters
|
|
371
379
|
|
package/dist/index.cjs
CHANGED
|
@@ -152,17 +152,33 @@ var KeyvAdapter = class {
|
|
|
152
152
|
var createCache = (options) => {
|
|
153
153
|
const eventEmitter = new import_node_events.default();
|
|
154
154
|
const stores = options?.stores?.length ? options.stores : [new import_keyv.Keyv()];
|
|
155
|
+
const nonBlocking = options?.nonBlocking ?? false;
|
|
155
156
|
const get = async (key) => {
|
|
156
|
-
|
|
157
|
+
let result = null;
|
|
158
|
+
if (nonBlocking) {
|
|
157
159
|
try {
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
return
|
|
160
|
+
result = await Promise.race(stores.map(async (store) => store.get(key)));
|
|
161
|
+
if (result === void 0) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
} catch (error) {
|
|
165
|
+
eventEmitter.emit("get", { key, error });
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
for (const store of stores) {
|
|
169
|
+
try {
|
|
170
|
+
const cacheValue = await store.get(key);
|
|
171
|
+
if (cacheValue !== void 0) {
|
|
172
|
+
result = cacheValue;
|
|
173
|
+
eventEmitter.emit("get", { key, value: result });
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
} catch (error) {
|
|
177
|
+
eventEmitter.emit("get", { key, error });
|
|
161
178
|
}
|
|
162
|
-
} catch {
|
|
163
179
|
}
|
|
164
180
|
}
|
|
165
|
-
return
|
|
181
|
+
return result;
|
|
166
182
|
};
|
|
167
183
|
const mget = async (keys) => {
|
|
168
184
|
const result = [];
|
|
@@ -174,6 +190,11 @@ var createCache = (options) => {
|
|
|
174
190
|
};
|
|
175
191
|
const set = async (stores2, key, value, ttl) => {
|
|
176
192
|
try {
|
|
193
|
+
if (nonBlocking) {
|
|
194
|
+
Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
|
|
195
|
+
eventEmitter.emit("set", { key, value });
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
177
198
|
await Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
|
|
178
199
|
eventEmitter.emit("set", { key, value });
|
|
179
200
|
return value;
|
|
@@ -199,6 +220,11 @@ var createCache = (options) => {
|
|
|
199
220
|
};
|
|
200
221
|
const del = async (key) => {
|
|
201
222
|
try {
|
|
223
|
+
if (nonBlocking) {
|
|
224
|
+
Promise.all(stores.map(async (store) => store.delete(key)));
|
|
225
|
+
eventEmitter.emit("del", { key });
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
202
228
|
await Promise.all(stores.map(async (store) => store.delete(key)));
|
|
203
229
|
eventEmitter.emit("del", { key });
|
|
204
230
|
return true;
|
|
@@ -213,6 +239,11 @@ var createCache = (options) => {
|
|
|
213
239
|
for (const key of keys) {
|
|
214
240
|
promises.push(stores.map(async (store) => store.delete(key)));
|
|
215
241
|
}
|
|
242
|
+
if (nonBlocking) {
|
|
243
|
+
Promise.all(promises);
|
|
244
|
+
eventEmitter.emit("mdel", { keys });
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
216
247
|
await Promise.all(promises);
|
|
217
248
|
eventEmitter.emit("mdel", { keys });
|
|
218
249
|
return true;
|
|
@@ -223,6 +254,11 @@ var createCache = (options) => {
|
|
|
223
254
|
};
|
|
224
255
|
const clear = async () => {
|
|
225
256
|
try {
|
|
257
|
+
if (nonBlocking) {
|
|
258
|
+
Promise.all(stores.map(async (store) => store.clear()));
|
|
259
|
+
eventEmitter.emit("clear");
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
226
262
|
await Promise.all(stores.map(async (store) => store.clear()));
|
|
227
263
|
eventEmitter.emit("clear");
|
|
228
264
|
return true;
|
package/dist/index.d.cts
CHANGED
|
@@ -34,6 +34,7 @@ type CreateCacheOptions = {
|
|
|
34
34
|
stores?: Keyv[];
|
|
35
35
|
ttl?: number;
|
|
36
36
|
refreshThreshold?: number;
|
|
37
|
+
nonBlocking?: boolean;
|
|
37
38
|
};
|
|
38
39
|
type Events = {
|
|
39
40
|
set: <T>(data: {
|
|
@@ -54,7 +55,7 @@ type Events = {
|
|
|
54
55
|
};
|
|
55
56
|
declare const createCache: (options?: CreateCacheOptions) => {
|
|
56
57
|
get: <T>(key: string) => Promise<T | null>;
|
|
57
|
-
mget: <T>(keys: string[]) => Promise<
|
|
58
|
+
mget: <T>(keys: string[]) => Promise<[T]>;
|
|
58
59
|
set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
|
|
59
60
|
mset: <T>(list: Array<{
|
|
60
61
|
key: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ type CreateCacheOptions = {
|
|
|
34
34
|
stores?: Keyv[];
|
|
35
35
|
ttl?: number;
|
|
36
36
|
refreshThreshold?: number;
|
|
37
|
+
nonBlocking?: boolean;
|
|
37
38
|
};
|
|
38
39
|
type Events = {
|
|
39
40
|
set: <T>(data: {
|
|
@@ -54,7 +55,7 @@ type Events = {
|
|
|
54
55
|
};
|
|
55
56
|
declare const createCache: (options?: CreateCacheOptions) => {
|
|
56
57
|
get: <T>(key: string) => Promise<T | null>;
|
|
57
|
-
mget: <T>(keys: string[]) => Promise<
|
|
58
|
+
mget: <T>(keys: string[]) => Promise<[T]>;
|
|
58
59
|
set: <T>(key: string, value: T, ttl?: number) => Promise<T>;
|
|
59
60
|
mset: <T>(list: Array<{
|
|
60
61
|
key: string;
|
package/dist/index.js
CHANGED
|
@@ -117,17 +117,33 @@ var KeyvAdapter = class {
|
|
|
117
117
|
var createCache = (options) => {
|
|
118
118
|
const eventEmitter = new EventEmitter();
|
|
119
119
|
const stores = options?.stores?.length ? options.stores : [new Keyv()];
|
|
120
|
+
const nonBlocking = options?.nonBlocking ?? false;
|
|
120
121
|
const get = async (key) => {
|
|
121
|
-
|
|
122
|
+
let result = null;
|
|
123
|
+
if (nonBlocking) {
|
|
122
124
|
try {
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
return
|
|
125
|
+
result = await Promise.race(stores.map(async (store) => store.get(key)));
|
|
126
|
+
if (result === void 0) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
} catch (error) {
|
|
130
|
+
eventEmitter.emit("get", { key, error });
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
for (const store of stores) {
|
|
134
|
+
try {
|
|
135
|
+
const cacheValue = await store.get(key);
|
|
136
|
+
if (cacheValue !== void 0) {
|
|
137
|
+
result = cacheValue;
|
|
138
|
+
eventEmitter.emit("get", { key, value: result });
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
eventEmitter.emit("get", { key, error });
|
|
126
143
|
}
|
|
127
|
-
} catch {
|
|
128
144
|
}
|
|
129
145
|
}
|
|
130
|
-
return
|
|
146
|
+
return result;
|
|
131
147
|
};
|
|
132
148
|
const mget = async (keys) => {
|
|
133
149
|
const result = [];
|
|
@@ -139,6 +155,11 @@ var createCache = (options) => {
|
|
|
139
155
|
};
|
|
140
156
|
const set = async (stores2, key, value, ttl) => {
|
|
141
157
|
try {
|
|
158
|
+
if (nonBlocking) {
|
|
159
|
+
Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
|
|
160
|
+
eventEmitter.emit("set", { key, value });
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
142
163
|
await Promise.all(stores2.map(async (store) => store.set(key, value, ttl ?? options?.ttl)));
|
|
143
164
|
eventEmitter.emit("set", { key, value });
|
|
144
165
|
return value;
|
|
@@ -164,6 +185,11 @@ var createCache = (options) => {
|
|
|
164
185
|
};
|
|
165
186
|
const del = async (key) => {
|
|
166
187
|
try {
|
|
188
|
+
if (nonBlocking) {
|
|
189
|
+
Promise.all(stores.map(async (store) => store.delete(key)));
|
|
190
|
+
eventEmitter.emit("del", { key });
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
167
193
|
await Promise.all(stores.map(async (store) => store.delete(key)));
|
|
168
194
|
eventEmitter.emit("del", { key });
|
|
169
195
|
return true;
|
|
@@ -178,6 +204,11 @@ var createCache = (options) => {
|
|
|
178
204
|
for (const key of keys) {
|
|
179
205
|
promises.push(stores.map(async (store) => store.delete(key)));
|
|
180
206
|
}
|
|
207
|
+
if (nonBlocking) {
|
|
208
|
+
Promise.all(promises);
|
|
209
|
+
eventEmitter.emit("mdel", { keys });
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
181
212
|
await Promise.all(promises);
|
|
182
213
|
eventEmitter.emit("mdel", { keys });
|
|
183
214
|
return true;
|
|
@@ -188,6 +219,11 @@ var createCache = (options) => {
|
|
|
188
219
|
};
|
|
189
220
|
const clear = async () => {
|
|
190
221
|
try {
|
|
222
|
+
if (nonBlocking) {
|
|
223
|
+
Promise.all(stores.map(async (store) => store.clear()));
|
|
224
|
+
eventEmitter.emit("clear");
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
191
227
|
await Promise.all(stores.map(async (store) => store.clear()));
|
|
192
228
|
eventEmitter.emit("clear");
|
|
193
229
|
return true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Cache Manager for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"keyv": "^5.0.3"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@faker-js/faker": "^9.0.
|
|
56
|
-
"@types/node": "^22.
|
|
55
|
+
"@faker-js/faker": "^9.0.2",
|
|
56
|
+
"@types/node": "^22.7.0",
|
|
57
57
|
"@vitest/coverage-v8": "^2.1.1",
|
|
58
|
-
"cache-manager-redis-yet": "^5.1.
|
|
58
|
+
"cache-manager-redis-yet": "^5.1.5",
|
|
59
59
|
"rimraf": "^6.0.1",
|
|
60
60
|
"tsup": "^8.3.0",
|
|
61
61
|
"typescript": "^5.6.2",
|