cache-manager 6.1.0 → 6.1.2
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 +36 -32
- package/package.json +13 -10
package/README.md
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
[<img align="center" src="https://cacheable.org/
|
|
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
|
[](https://codecov.io/gh/jaredwray/cacheable)
|
|
5
5
|
[](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)
|
|
6
|
-
[](https://github.com/jaredwray/cacheable/blob/main/LICENSE)
|
|
7
6
|
[](https://npmjs.com/package/cache-manager)
|
|
8
|
-

|
|
7
|
+
[](https://npmjs.com/package/cache-manager)
|
|
8
|
+
[](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)
|
|
@@ -46,7 +48,7 @@ If you are looking for older documentation you can find it here:
|
|
|
46
48
|
* [Contribute](#contribute)
|
|
47
49
|
* [License](#license)
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
# Installation
|
|
50
52
|
|
|
51
53
|
```sh
|
|
52
54
|
npm install cache-manager
|
|
@@ -65,7 +67,7 @@ npm install @keyv/etcd
|
|
|
65
67
|
```
|
|
66
68
|
In addition Keyv supports other storage adapters such as `lru-cache` and `CacheableMemory` from Cacheable (more examples below). Please read [Keyv document](https://keyv.org/docs/) for more information.
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
# Quick start
|
|
69
71
|
```typescript
|
|
70
72
|
import { Keyv } from 'keyv';
|
|
71
73
|
import { createCache } from 'cache-manager';
|
|
@@ -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
|
|
87
|
+
import KeyvRedis from '@keyv/redis';
|
|
86
88
|
import { CacheableMemory } from 'cacheable';
|
|
87
89
|
import { createCache } from 'cache-manager';
|
|
88
90
|
|
|
@@ -128,7 +130,7 @@ await cache.wrap('key', () => 'value')
|
|
|
128
130
|
// => value
|
|
129
131
|
```
|
|
130
132
|
|
|
131
|
-
|
|
133
|
+
# Using CacheableMemory or lru-cache as storage adapter
|
|
132
134
|
|
|
133
135
|
Because we are using [Keyv](https://keyv.org/), you can use any storage adapter that Keyv supports such as `lru-cache` or `CacheableMemory` from Cacheable. Below is an example of using `CacheableMemory`:
|
|
134
136
|
|
|
@@ -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 {
|
|
141
|
+
import { Keyv } from 'keyv';
|
|
142
|
+
import { KeyvCacheableMemory } from 'cacheable';
|
|
141
143
|
|
|
142
|
-
const
|
|
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
|
|
|
@@ -154,7 +157,7 @@ const keyv = new Keyv({ store: new LRU({ max: 5000, maxAge: 60000 }) });
|
|
|
154
157
|
const cache = createCache({ stores: [keyv] });
|
|
155
158
|
```
|
|
156
159
|
|
|
157
|
-
|
|
160
|
+
## Options
|
|
158
161
|
- **stores**?: Keyv[]
|
|
159
162
|
|
|
160
163
|
List of Keyv instance. Please refer to the [Keyv document](https://keyv.org/docs/#3.-create-a-new-keyv-instance) for more information.
|
|
@@ -167,17 +170,18 @@ const cache = createCache({ stores: [keyv] });
|
|
|
167
170
|
|
|
168
171
|
- **nonBlocking**?: boolean - Default false
|
|
169
172
|
|
|
170
|
-
If set to true, the system will not block when multiple stores are used. Here is how it affects
|
|
173
|
+
If set to true, the system will not block when multiple stores are used. Here is how it affects the type of functions:
|
|
171
174
|
* `set and mset` - will not wait for all stores to finish.
|
|
172
175
|
* `get and mget` - will return the first (fastest) value found.
|
|
173
176
|
* `del and mdel` - will not wait for all stores to finish.
|
|
174
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).
|
|
175
179
|
|
|
176
|
-
|
|
177
|
-
|
|
180
|
+
# Methods
|
|
181
|
+
## set
|
|
178
182
|
`set(key, value, [ttl]): Promise<value>`
|
|
179
183
|
|
|
180
|
-
Sets a key value pair. It is possible to define a ttl (in
|
|
184
|
+
Sets a key value pair. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
|
|
181
185
|
|
|
182
186
|
```ts
|
|
183
187
|
await cache.set('key-1', 'value 1')
|
|
@@ -187,11 +191,11 @@ await cache.set('key 2', 'value 2', 5000)
|
|
|
187
191
|
```
|
|
188
192
|
See unit tests in [`test/set.test.ts`](./test/set.test.ts) for more information.
|
|
189
193
|
|
|
190
|
-
|
|
194
|
+
## mset
|
|
191
195
|
|
|
192
196
|
`mset(keys: [ { key, value, ttl } ]): Promise<true>`
|
|
193
197
|
|
|
194
|
-
Sets multiple key value pairs. It is possible to define a ttl (in
|
|
198
|
+
Sets multiple key value pairs. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
|
|
195
199
|
|
|
196
200
|
```ts
|
|
197
201
|
await cache.mset([
|
|
@@ -200,7 +204,7 @@ await cache.mset([
|
|
|
200
204
|
]);
|
|
201
205
|
```
|
|
202
206
|
|
|
203
|
-
|
|
207
|
+
## get
|
|
204
208
|
`get(key): Promise<value>`
|
|
205
209
|
|
|
206
210
|
Gets a saved value from the cache. Returns a null if not found or expired. If the value was found it returns the value.
|
|
@@ -216,7 +220,7 @@ await cache.get('foo')
|
|
|
216
220
|
```
|
|
217
221
|
See unit tests in [`test/get.test.ts`](./test/get.test.ts) for more information.
|
|
218
222
|
|
|
219
|
-
|
|
223
|
+
## mget
|
|
220
224
|
|
|
221
225
|
`mget(keys: [key]): Promise<value[]>`
|
|
222
226
|
|
|
@@ -232,7 +236,7 @@ await cache.mget(['key-1', 'key-2', 'key-3'])
|
|
|
232
236
|
// => ['value 1', 'value 2', null]
|
|
233
237
|
```
|
|
234
238
|
|
|
235
|
-
|
|
239
|
+
## del
|
|
236
240
|
`del(key): Promise<true>`
|
|
237
241
|
|
|
238
242
|
Delete a key, an error will be throw on any failed.
|
|
@@ -250,7 +254,7 @@ await cache.get('key')
|
|
|
250
254
|
```
|
|
251
255
|
See unit tests in [`test/del.test.ts`](./test/del.test.ts) for more information.
|
|
252
256
|
|
|
253
|
-
|
|
257
|
+
## mdel
|
|
254
258
|
|
|
255
259
|
`mdel(keys: [key]): Promise<true>`
|
|
256
260
|
|
|
@@ -265,7 +269,7 @@ await cache.mset([
|
|
|
265
269
|
await cache.mdel(['key-1', 'key-2'])
|
|
266
270
|
```
|
|
267
271
|
|
|
268
|
-
|
|
272
|
+
## clear
|
|
269
273
|
`clear(): Promise<true>`
|
|
270
274
|
|
|
271
275
|
Flush all data, an error will be throw on any failed.
|
|
@@ -288,7 +292,7 @@ await cache.get('key-2')
|
|
|
288
292
|
```
|
|
289
293
|
See unit tests in [`test/clear.test.ts`](./test/clear.test.ts) for more information.
|
|
290
294
|
|
|
291
|
-
|
|
295
|
+
## wrap
|
|
292
296
|
`wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>`
|
|
293
297
|
|
|
294
298
|
Wraps a function in cache. The first time the function is run, its results are stored in cache so subsequent calls retrieve from cache instead of calling the function.
|
|
@@ -328,8 +332,8 @@ await cache.wrap('error', () => {
|
|
|
328
332
|
|
|
329
333
|
See unit tests in [`test/wrap.test.ts`](./test/wrap.test.ts) for more information.
|
|
330
334
|
|
|
331
|
-
|
|
332
|
-
|
|
335
|
+
# Events
|
|
336
|
+
## set
|
|
333
337
|
Fired when a key has been added or changed.
|
|
334
338
|
|
|
335
339
|
```ts
|
|
@@ -338,7 +342,7 @@ cache.on('set', ({ key, value, error }) => {
|
|
|
338
342
|
})
|
|
339
343
|
```
|
|
340
344
|
|
|
341
|
-
|
|
345
|
+
## del
|
|
342
346
|
Fired when a key has been removed manually.
|
|
343
347
|
|
|
344
348
|
```ts
|
|
@@ -347,7 +351,7 @@ cache.on('del', ({ key, error }) => {
|
|
|
347
351
|
})
|
|
348
352
|
```
|
|
349
353
|
|
|
350
|
-
|
|
354
|
+
## clear
|
|
351
355
|
Fired when the cache has been flushed.
|
|
352
356
|
|
|
353
357
|
```ts
|
|
@@ -358,7 +362,7 @@ cache.on('clear', (error) => {
|
|
|
358
362
|
})
|
|
359
363
|
```
|
|
360
364
|
|
|
361
|
-
|
|
365
|
+
## refresh
|
|
362
366
|
Fired when the cache has been refreshed in the background.
|
|
363
367
|
|
|
364
368
|
```ts
|
|
@@ -371,11 +375,11 @@ cache.on('refresh', ({ key, value, error }) => {
|
|
|
371
375
|
|
|
372
376
|
See unit tests in [`test/events.test.ts`](./test/events.test.ts) for more information.
|
|
373
377
|
|
|
374
|
-
|
|
378
|
+
# Update on redis and ioredis Support
|
|
375
379
|
|
|
376
380
|
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`.
|
|
377
381
|
|
|
378
|
-
|
|
382
|
+
# Using Legacy Storage Adapters
|
|
379
383
|
|
|
380
384
|
There are many storage adapters built for `cache-manager` and because of that we wanted to provide a way to use them with `KeyvAdapter`. Below is an example of using `cache-manager-redis-yet`:
|
|
381
385
|
|
|
@@ -392,10 +396,10 @@ const cache = createCache({
|
|
|
392
396
|
|
|
393
397
|
This adapter will allow you to add in any storage adapter. If there are issues it needs to follow `CacheManagerStore` interface.
|
|
394
398
|
|
|
395
|
-
|
|
399
|
+
# Contribute
|
|
396
400
|
|
|
397
401
|
If you would like to contribute to the project, please read how to contribute here [CONTRIBUTING.md](https://github.com/jaredwray/cacheable/blob/main/CONTRIBUTING.md).
|
|
398
402
|
|
|
399
|
-
|
|
403
|
+
# License
|
|
400
404
|
|
|
401
405
|
[MIT © Jared Wray ](./LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "6.1.
|
|
3
|
+
"version": "6.1.2",
|
|
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.
|
|
53
|
+
"keyv": "^5.1.2"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@faker-js/faker": "^9.0
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
56
|
+
"@faker-js/faker": "^9.1.0",
|
|
57
|
+
"@keyv/redis": "^3.0.1",
|
|
58
|
+
"@types/node": "^22.8.1",
|
|
59
|
+
"@vitest/coverage-v8": "^2.1.3",
|
|
58
60
|
"cache-manager-redis-yet": "^5.1.5",
|
|
61
|
+
"cacheable": "^1.8.1",
|
|
59
62
|
"rimraf": "^6.0.1",
|
|
60
|
-
"tsup": "^8.3.
|
|
61
|
-
"typescript": "^5.6.
|
|
62
|
-
"vitest": "^2.1.
|
|
63
|
+
"tsup": "^8.3.5",
|
|
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
|
|
77
|
+
"test:ci": "xo && vitest run",
|
|
75
78
|
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean"
|
|
76
79
|
}
|
|
77
80
|
}
|