cache-manager 6.0.1 → 6.1.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.
package/README.md CHANGED
@@ -1,17 +1,18 @@
1
- [<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
1
+ [<img align="center" src="https://cacheable.org/symbol.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable)
2
2
 
3
3
  # cache-manager
4
4
  [![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)
5
5
  [![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
6
- [![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
7
6
  [![npm](https://img.shields.io/npm/dm/cache-manager)](https://npmjs.com/package/cache-manager)
8
- ![npm](https://img.shields.io/npm/v/cache-manager)
7
+ [![npm](https://img.shields.io/npm/v/cache-manager)](https://npmjs.com/package/cache-manager)
8
+ [![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
9
9
 
10
10
  # Simple and fast NodeJS caching module.
11
11
  A cache module for NodeJS that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.
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
+ - `nonBlocking` option that optimizes how the system handles multiple stores.
15
16
  - Use with any [Keyv](https://keyv.org/) compatible storage adapter.
16
17
  - 100% test coverage via [vitest](https://github.com/vitest-dev/vitest).
17
18
 
@@ -27,6 +28,7 @@ If you are looking for older documentation you can find it here:
27
28
  * [Installation](#installation)
28
29
  * [Quick start](#quick-start)
29
30
  * [Using `CacheableMemory` or `lru-cache` as storage adapter](#using-cacheablememory-or-lru-cache-as-storage-adapter)
31
+ * [Options](#options)
30
32
  * [Methods](#methods)
31
33
  * [.set](#set)
32
34
  * [.mset](#mset)
@@ -82,7 +84,7 @@ Here is an example of doing layer 1 and layer 2 caching with the in-memory being
82
84
 
83
85
  ```ts
84
86
  import { Keyv } from 'keyv';
85
- import { KeyvRedis } from '@keyv/redis';
87
+ import KeyvRedis from '@keyv/redis';
86
88
  import { CacheableMemory } from 'cacheable';
87
89
  import { createCache } from 'cache-manager';
88
90
 
@@ -136,10 +138,11 @@ In this example we are using `CacheableMemory` from Cacheable which is a fast in
136
138
 
137
139
  ```ts
138
140
  import { createCache } from 'cache-manager';
139
- import {Keyv} from 'keyv';
140
- import { CacheableMemory } from 'cacheable';
141
+ import { Keyv } from 'keyv';
142
+ import { KeyvCacheableMemory } from 'cacheable';
141
143
 
142
- const keyv = new Keyv({ store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }) });
144
+ const store = new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 });
145
+ const keyv = new Keyv({ store });
143
146
  const cache = createCache({ stores: [keyv] });
144
147
  ```
145
148
 
@@ -165,11 +168,20 @@ const cache = createCache({ stores: [keyv] });
165
168
 
166
169
  If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.
167
170
 
171
+ - **nonBlocking**?: boolean - Default false
172
+
173
+ If set to true, the system will not block when multiple stores are used. Here is how it affects the type of functions:
174
+ * `set and mset` - will not wait for all stores to finish.
175
+ * `get and mget` - will return the first (fastest) value found.
176
+ * `del and mdel` - will not wait for all stores to finish.
177
+ * `clear` - will not wait for all stores to finish.
178
+ * `wrap` - will do the same as `get` and `set` (return the first value found and not wait for all stores to finish).
179
+
168
180
  ## Methods
169
181
  ### set
170
182
  `set(key, value, [ttl]): Promise<value>`
171
183
 
172
- Sets a key value pair. It is possible to define a ttl (in miliseconds). An error will be throw on any failed
184
+ Sets a key value pair. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
173
185
 
174
186
  ```ts
175
187
  await cache.set('key-1', 'value 1')
@@ -183,7 +195,7 @@ See unit tests in [`test/set.test.ts`](./test/set.test.ts) for more information.
183
195
 
184
196
  `mset(keys: [ { key, value, ttl } ]): Promise<true>`
185
197
 
186
- Sets multiple key value pairs. It is possible to define a ttl (in miliseconds). An error will be throw on any failed
198
+ Sets multiple key value pairs. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
187
199
 
188
200
  ```ts
189
201
  await cache.mset([
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
- for (const store of stores) {
157
+ let result = null;
158
+ if (nonBlocking) {
157
159
  try {
158
- const data = await store.get(key);
159
- if (data !== void 0) {
160
- return data;
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 null;
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<(Awaited<T> | null)[]>;
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<(Awaited<T> | null)[]>;
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
- for (const store of stores) {
122
+ let result = null;
123
+ if (nonBlocking) {
122
124
  try {
123
- const data = await store.get(key);
124
- if (data !== void 0) {
125
- return data;
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 null;
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.0.1",
3
+ "version": "6.1.1",
4
4
  "description": "Cache Manager for Node.js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "git+https://github.com/jaredwray/cacheable.git"
21
+ "url": "git+https://github.com/jaredwray/cacheable.git",
22
+ "directory": "packages/cache-manager"
22
23
  },
23
24
  "keywords": [
24
25
  "cache",
@@ -49,17 +50,19 @@
49
50
  ],
50
51
  "license": "MIT",
51
52
  "dependencies": {
52
- "keyv": "^5.0.3"
53
+ "keyv": "^5.1.0"
53
54
  },
54
55
  "devDependencies": {
55
- "@faker-js/faker": "^9.0.2",
56
- "@types/node": "^22.6.1",
57
- "@vitest/coverage-v8": "^2.1.1",
56
+ "@faker-js/faker": "^9.0.3",
57
+ "@keyv/redis": "^3.0.1",
58
+ "@types/node": "^22.7.5",
59
+ "@vitest/coverage-v8": "^2.1.3",
58
60
  "cache-manager-redis-yet": "^5.1.5",
61
+ "cacheable": "^1.8.0",
59
62
  "rimraf": "^6.0.1",
60
63
  "tsup": "^8.3.0",
61
- "typescript": "^5.6.2",
62
- "vitest": "^2.1.1",
64
+ "typescript": "^5.6.3",
65
+ "vitest": "^2.1.3",
63
66
  "xo": "^0.59.3"
64
67
  },
65
68
  "xo": {
@@ -71,7 +74,7 @@
71
74
  "scripts": {
72
75
  "clean": "rimraf ./dist ./coverage ./node_modules",
73
76
  "test": "xo --fix && vitest run --coverage",
74
- "test:ci": "xo && vitest run --coverage",
77
+ "test:ci": "xo && vitest run",
75
78
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean"
76
79
  }
77
80
  }