cache-manager 5.7.5 → 6.0.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- MIT License
1
+ MIT License & © Jared Wray
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [<img align="center" src="https://jaredwray.com/images/cacheable_white.svg" alt="keyv">](https://github.com/jaredwray/cacheable)
1
+ [<img align="center" src="https://cacheable.org/logo.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)
@@ -7,340 +7,387 @@
7
7
  [![npm](https://img.shields.io/npm/dm/cache-manager)](https://npmjs.com/package/cache-manager)
8
8
  ![npm](https://img.shields.io/npm/v/cache-manager)
9
9
 
10
- # Flexible NodeJS cache module
10
+ # Simple and fast NodeJS caching module.
11
+ A cache module for NodeJS that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.
12
+ - Made with Typescript and compatible with [ESModules](https://nodejs.org/docs/latest-v14.x/api/esm.html).
13
+ - Easy way to wrap any function in cache, supports a mechanism to refresh expiring cache keys in background.
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/)-compatible storage adapter.
16
+ - 100% test coverage via [vitest](https://github.com/vitest-dev/vitest).
17
+
18
+ We moved to using [Keyv](https://keyv.org/) as the storage adapter which allows for a more supported and flexible storage adapter. These adapters have a larger community and are more actively maintained.
11
19
 
12
- A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface. This module is now part of the [Cacheable](https://cacheable.org) project.
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` who wrote `promise-coalesce` which was used in v5 and now embedded in v6.
21
+
22
+ If you are looking for older documentation you can find it here:
23
+ * [v5](./READMEv5.md)
24
+ * [v4](./READMEv4.md)
13
25
 
14
26
  ## Table of Contents
15
- * [Features](#features)
16
27
  * [Installation](#installation)
17
- * [Usage Examples](#usage-examples)
18
- * [Single Store](#single-store)
19
- * [Multi-Store](#multi-store)
20
- * [Cache Manager Options](#cache-manager-options)
21
- * [Refresh cache keys in background](#refresh-cache-keys-in-background)
22
- * [Error Handling](#error-handling)
23
- * [Express Middleware](#express-middleware)
24
- * [Store Engines](#store-engines)
28
+ * [Quick start](#quick-start)
29
+ * [Using `CacheableMemory` or `lru-cache` as storage adapter](#using-cacheablememory-or-lru-cache-as-storage-adapter)
30
+ * [Methods](#methods)
31
+ * [.set](#set)
32
+ * [.mset](#mset)
33
+ * [.get](#get)
34
+ * [.mget](#mget)
35
+ * [.del](#del)
36
+ * [.mdel](#mdel)
37
+ * [.clear](#clear)
38
+ * [.wrap](#wrap)
39
+ * [Events](#events)
40
+ * [.set](#set)
41
+ * [.del](#del)
42
+ * [.clear](#clear)
43
+ * [.refresh](#refresh)
44
+ * [Update on `redis` and `ioredis` Support](#update-on-redis-and-ioredis-support)
45
+ * [Using Legacy Storage Adapters](#using-legacy-storage-adapters)
25
46
  * [Contribute](#contribute)
26
47
  * [License](#license)
27
48
 
28
- ## Features
29
-
30
- - Made with Typescript and compatible with [ESModules](https://nodejs.org/docs/latest-v14.x/api/esm.html)
31
- - Easy way to wrap any function in cache.
32
- - Tiered caches -- data gets stored in each cache and fetched from the highest.
33
- priority cache(s) first.
34
- - Use any cache you want, as long as it has the same API.
35
- - 100% test coverage via [vitest](https://github.com/vitest-dev/vitest).
36
-
37
49
  ## Installation
38
50
 
39
- pnpm install cache-manager
51
+ ```sh
52
+ npm install cache-manager
53
+ ```
40
54
 
41
- ## Usage Examples
55
+ By default, everything is stored in memory; you can optionally also install a storage adapter; choose one from any of the storage adapters supported by Keyv:
42
56
 
43
- ### Single Store
57
+ ```sh
58
+ npm install @keyv/redis
59
+ npm install @keyv/memcache
60
+ npm install @keyv/mongo
61
+ npm install @keyv/sqlite
62
+ npm install @keyv/postgres
63
+ npm install @keyv/mysql
64
+ npm install @keyv/etcd
65
+ ```
66
+ 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.
44
67
 
68
+ ## Quick start
45
69
  ```typescript
46
- import { caching } from 'cache-manager';
70
+ import { Keyv } from 'keyv';
71
+ import { createCache } from 'cache-manager';
47
72
 
48
- const memoryCache = await caching('memory', {
49
- max: 100,
50
- ttl: 10 * 1000 /*milliseconds*/,
51
- });
73
+ // Memory store by default
74
+ const cache = createCache()
75
+
76
+ // Single store which is in memory
77
+ const cache = createCache({
78
+ stores: [new Keyv()],
79
+ })
80
+ ```
81
+ Here is an example of doing layer 1 and layer 2 caching with the in-memory being `CacheableMemory` from Cacheable and the second layer being `@keyv/redis`:
82
+
83
+ ```ts
84
+ import { Keyv } from 'keyv';
85
+ import { KeyvRedis } from '@keyv/redis';
86
+ import { CacheableMemory } from 'cacheable';
87
+ import { createCache } from 'cache-manager';
88
+
89
+ // Multiple stores
90
+ const cache = createCache({
91
+ stores: [
92
+ // High performance in-memory cache with LRU and TTL
93
+ new Keyv({
94
+ store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }),
95
+ }),
96
+
97
+ // Redis Store
98
+ new Keyv({
99
+ store: new KeyvRedis('redis://user:pass@localhost:6379'),
100
+ }),
101
+ ],
102
+ })
103
+ ```
104
+
105
+ Once it is created, you can use the cache object to set, get, delete, and wrap functions in cache.
52
106
 
53
- const ttl = 5 * 1000; /*milliseconds*/
54
- await memoryCache.set('foo', 'bar', ttl);
107
+ ```ts
55
108
 
56
- console.log(await memoryCache.get('foo'));
57
- // >> "bar"
109
+ // With default ttl and refreshThreshold
110
+ const cache = createCache({
111
+ ttl: 10000,
112
+ refreshThreshold: 3000,
113
+ })
58
114
 
59
- await memoryCache.del('foo');
115
+ await cache.set('foo', 'bar')
116
+ // => bar
60
117
 
61
- console.log(await memoryCache.get('foo'));
62
- // >> undefined
118
+ await cache.get('foo')
119
+ // => bar
63
120
 
64
- const getUser = (id: string) => new Promise.resolve({ id: id, name: 'Bob' });
121
+ await cache.del('foo')
122
+ // => true
65
123
 
66
- const userId = 123;
67
- const key = 'user_' + userId;
124
+ await cache.get('foo')
125
+ // => null
68
126
 
69
- console.log(await memoryCache.wrap(key, () => getUser(userId), ttl));
70
- // >> { id: 123, name: 'Bob' }
127
+ await cache.wrap('key', () => 'value')
128
+ // => value
71
129
  ```
72
130
 
73
- See unit tests in [`test/caching.test.ts`](./test/caching.test.ts) for more information.
131
+ ## Using `CacheableMemory` or `lru-cache` as storage adapter
74
132
 
75
- #### Example setting/getting several keys with mset() and mget()
133
+ 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`:
76
134
 
77
- ```typescript
78
- await memoryCache.store.mset(
79
- [
80
- ['foo', 'bar'],
81
- ['foo2', 'bar2'],
82
- ],
83
- ttl,
84
- );
135
+ In this example we are using `CacheableMemory` from Cacheable which is a fast in-memory cache that supports LRU and and TTL expiration.
85
136
 
86
- console.log(await memoryCache.store.mget('foo', 'foo2'));
87
- // >> ['bar', 'bar2']
137
+ ```ts
138
+ import { createCache } from 'cache-manager';
139
+ import {Keyv} from 'keyv';
140
+ import { CacheableMemory } from 'cacheable';
88
141
 
89
- // Delete keys with mdel() passing arguments...
90
- await memoryCache.store.mdel('foo', 'foo2');
142
+ const keyv = new Keyv({ store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }) });
143
+ const cache = createCache({ stores: [keyv] });
91
144
  ```
92
145
 
93
- #### Custom Stores
146
+ Here is an example using `lru-cache`:
94
147
 
95
- You can use your own custom store by creating one with the same API as the built-in memory stores.
148
+ ```ts
149
+ import { createCache } from 'cache-manager';
150
+ import { Keyv } from 'keyv';
151
+ import { LRU } from 'lru-cache';
96
152
 
97
- - [Example Custom Store lru-cache](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/src/stores/memory.ts)
98
- - [Example Custom Store redis](https://github.com/jaredwray/cacheable/tree/main/packages/cache-manager-redis-yet)
99
- - [Example Custom Store ioredis](https://github.com/jaredwray/cacheable/tree/main/packages/cache-manager-ioredis-yet)
153
+ const keyv = new Keyv({ store: new LRU({ max: 5000, maxAge: 60000 }) });
154
+ const cache = createCache({ stores: [keyv] });
155
+ ```
100
156
 
101
- #### Create single cache store synchronously
157
+ ### Options
158
+ - **stores**?: Keyv[]
102
159
 
103
- As `caching()` requires async functionality to resolve some stores, this is not well-suited to use for default function/constructor parameters etc.
160
+ List of Keyv instance. Please refer to the [Keyv document](https://keyv.org/docs/#3.-create-a-new-keyv-instance) for more information.
161
+ - **ttl**?: number - Default time to live in milliseconds.
104
162
 
105
- If you need to create a cache store synchronously, you can instead use `createCache()`:
163
+ The time to live in milliseconds. This is the maximum amount of time that an item can be in the cache before it is removed.
164
+ - **refreshThreshold**?: number - Default refreshThreshold in milliseconds.
106
165
 
107
- ```typescript
108
- import { createCache, memoryStore } from 'cache-manager';
109
-
110
- // Create memory cache synchronously
111
- const memoryCache = createCache(memoryStore({
112
- max: 100,
113
- ttl: 10 * 1000 /*milliseconds*/,
114
- }));
115
-
116
- // Default parameter in function
117
- function myService(cache = createCache(memoryStore())) {}
118
-
119
- // Default parameter in class constructor
120
- const DEFAULT_CACHE = createCache(memoryStore(), { ttl: 60 * 1000 });
121
- // ...
122
- class MyService {
123
- constructor(private cache = DEFAULT_CACHE) {}
124
- }
125
- ```
166
+ If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.
126
167
 
127
- ### Multi-Store
168
+ ## Methods
169
+ ### set
170
+ `set(key, value, [ttl]): Promise<value>`
128
171
 
129
- ```typescript
130
- import { multiCaching } from 'cache-manager';
172
+ Sets a key value pair. It is possible to define a ttl (in miliseconds). An error will be throw on any failed
173
+
174
+ ```ts
175
+ await cache.set('key-1', 'value 1')
131
176
 
132
- const multiCache = multiCaching([memoryCache, someOtherCache]);
133
- const userId2 = 456;
134
- const key2 = 'user_' + userId;
135
- const ttl = 5;
177
+ // expires after 5 seconds
178
+ await cache.set('key 2', 'value 2', 5000)
179
+ ```
180
+ See unit tests in [`test/set.test.ts`](./test/set.test.ts) for more information.
136
181
 
137
- // Sets in all caches.
138
- await multiCache.set('foo2', 'bar2', ttl);
182
+ ### mset
139
183
 
140
- // Fetches from highest priority cache that has the key.
141
- console.log(await multiCache.get('foo2'));
142
- // >> "bar2"
184
+ `mset(keys: [ { key, value, ttl } ]): Promise<true>`
143
185
 
144
- // Delete from all caches
145
- await multiCache.del('foo2');
186
+ Sets multiple key value pairs. It is possible to define a ttl (in miliseconds). An error will be throw on any failed
146
187
 
147
- // Sets multiple keys in all caches.
148
- // You can pass as many key, value tuples as you want
149
- await multiCache.mset(
150
- [
151
- ['foo', 'bar'],
152
- ['foo2', 'bar2'],
153
- ],
154
- ttl
155
- );
156
-
157
- // mget() fetches from highest priority cache.
158
- // If the first cache does not return all the keys,
159
- // the next cache is fetched with the keys that were not found.
160
- // This is done recursively until either:
161
- // - all have been found
162
- // - all caches has been fetched
163
- console.log(await multiCache.mget('key', 'key2'));
164
- // >> ['bar', 'bar2']
165
-
166
- // Delete keys with mdel() passing arguments...
167
- await multiCache.mdel('foo', 'foo2');
188
+ ```ts
189
+ await cache.mset([
190
+ { key: 'key-1', value: 'value 1' },
191
+ { key: 'key-2', value: 'value 2', ttl: 5000 },
192
+ ]);
168
193
  ```
169
194
 
170
- See unit tests in [`test/multi-caching.test.ts`](./test/multi-caching.test.ts) for more information.
195
+ ### get
196
+ `get(key): Promise<value>`
171
197
 
172
- ### Cache Manager Options
198
+ Gets a saved value from the cache. Returns a null if not found or expired. If the value was found it returns the value.
173
199
 
174
- The `caching` function accepts an options object as the second parameter. The following options are available:
175
- * ttl: The time to live in milliseconds. This is the maximum amount of time that an item can be in the cache before it is removed.
176
- * refreshThreshold: discussed in details below.
177
- * isCacheable: a function to determine whether the value is cacheable or not.
178
- * onBackgroundRefreshError: a function to handle errors that occur during background refresh.
200
+ ```ts
201
+ await cache.set('key', 'value')
179
202
 
180
- ```typescript
181
- import { caching } from 'cache-manager';
203
+ await cache.get('key')
204
+ // => value
182
205
 
183
- const memoryCache = await caching('memory', {
184
- max: 100,
185
- ttl: 10 * 1000 /*milliseconds*/,
186
- shouldCloneBeforeSet: false, // this is set true by default (optional)
187
- });
206
+ await cache.get('foo')
207
+ // => null
188
208
  ```
209
+ See unit tests in [`test/get.test.ts`](./test/get.test.ts) for more information.
189
210
 
190
- When creating a memory store, you also get these addition options:
191
- * max: The maximum number of items that can be stored in the cache. If the cache is full, the least recently used item is removed.
192
- * shouldCloneBeforeSet: If true, the value will be cloned before being set in the cache. This is set to `true` by default.
211
+ ### mget
193
212
 
194
- ### Refresh cache keys in background
213
+ `mget(keys: [key]): Promise<value[]>`
195
214
 
196
- Both the `caching` and `multicaching` modules support a mechanism to refresh expiring cache keys in background when using the `wrap` function.
197
- This is done by adding a `refreshThreshold` attribute while creating the caching store or passing it to the `wrap` function.
215
+ Gets multiple saved values from the cache. Returns a null if not found or expired. If the value was found it returns the value.
198
216
 
199
- If `refreshThreshold` is set and after retrieving a value from cache the TTL will be checked.
200
- If the remaining TTL is less than `refreshThreshold`, the system will update the value asynchronously,
201
- following same rules as standard fetching. In the meantime, the system will return the old value until expiration.
217
+ ```ts
218
+ await cache.mset([
219
+ { key: 'key-1', value: 'value 1' },
220
+ { key: 'key-2', value: 'value 2' },
221
+ ]);
202
222
 
203
- NOTES:
223
+ await cache.mget(['key-1', 'key-2', 'key-3'])
224
+ // => ['value 1', 'value 2', null]
225
+ ```
204
226
 
205
- * In case of multicaching, the store that will be checked for refresh is the one where the key will be found first (highest priority).
206
- * If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values.
207
- * The background refresh mechanism currently does not support providing multiple keys to `wrap` function.
208
- * If no `ttl` is set for the key, the refresh mechanism will not be triggered. For redis, the `ttl` is set to -1 by default.
227
+ ### del
228
+ `del(key): Promise<true>`
209
229
 
210
- For example, pass the refreshThreshold to `caching` like this:
230
+ Delete a key, an error will be throw on any failed.
211
231
 
212
- ```typescript
213
- const memoryCache = await caching('memory', {
214
- max: 100,
215
- ttl: 10 * 1000 /*milliseconds*/,
216
- refreshThreshold: 3 * 1000 /*milliseconds*/,
217
-
218
- /* optional, but if not set, background refresh error will be an unhandled
219
- * promise rejection, which might crash your node process */
220
- onBackgroundRefreshError: (error) => { /* log or otherwise handle error */ }
221
- });
232
+ ```ts
233
+ await cache.set('key', 'value')
234
+
235
+ await cache.get('key')
236
+ // => value
237
+
238
+ await cache.del('key')
239
+
240
+ await cache.get('key')
241
+ // => null
222
242
  ```
243
+ See unit tests in [`test/del.test.ts`](./test/del.test.ts) for more information.
223
244
 
224
- When a value will be retrieved from Redis with a TTL minor than 3sec, the value will be updated in the background.
245
+ ### mdel
225
246
 
226
- ## Error Handling
247
+ `mdel(keys: [key]): Promise<true>`
227
248
 
228
- `multiCaching` now does not throw errors by default. Instead, all errors are evented through the `error` event. Here is an example on how to use it:
249
+ Delete multiple keys, an error will be throw on any failed.
229
250
 
230
- ```javascript
231
- const multicache = await multiCaching([memoryCache, someOtherCache]);
232
- multicache.on('error', (error) => {
233
- console.error('Cache error:', error);
234
- });
251
+ ```ts
252
+ await cache.mset([
253
+ { key: 'key-1', value: 'value 1' },
254
+ { key: 'key-2', value: 'value 2' },
255
+ ]);
256
+
257
+ await cache.mdel(['key-1', 'key-2'])
235
258
  ```
236
259
 
237
- ## Using non-blocking set with wrap
238
- By default, when using `wrap` the value is set in the cache before the function returns.
239
- While this behaviour can prevent additional calls to downstream resources, it can also slow down the response time.
240
- This can be changed by setting the `nonBlockingSet` option to `true`.
241
- Doing will make the function return before the value is set in the cache.
242
- The setting applies to both single and multi caches.
260
+ ### clear
261
+ `clear(): Promise<true>`
243
262
 
244
- ```typescript
245
- cache.wrap('key', () => fetchValue(), 1000, 500, {nonBlockingSet: true});
263
+ Flush all data, an error will be throw on any failed.
264
+
265
+ ```ts
266
+ await cache.set('key-1', 'value 1')
267
+ await cache.set('key-2', 'value 2')
268
+
269
+ await cache.get('key-1')
270
+ // => value 1
271
+ await cache.get('key-2')
272
+ // => value 2
273
+
274
+ await cache.clear()
275
+
276
+ await cache.get('key-1')
277
+ // => null
278
+ await cache.get('key-2')
279
+ // => null
246
280
  ```
281
+ See unit tests in [`test/clear.test.ts`](./test/clear.test.ts) for more information.
247
282
 
248
- ## Express Middleware
283
+ ### wrap
284
+ `wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>`
249
285
 
250
- This example sets up an Express application with a caching mechanism using cache-manager. The cacheMiddleware checks if the response for a request is already cached and returns it if available. If not, it proceeds to the route handler, caches the response, and then returns it. This helps to reduce the load on the server by avoiding repeated processing of the same requests.
286
+ 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.
287
+
288
+ If `refreshThreshold` is set and the remaining TTL is less than `refreshThreshold`, the system will update the value asynchronously. In the meantime, the system will return the old value until expiration.
251
289
 
252
290
  ```typescript
253
- // The code imports the necessary modules using ES module syntax:
254
- import { caching } from 'cache-manager';
255
- import express from 'express';
256
-
257
- // The memory cache is initialized using cache-manager with a maximum of 100 items and a TTL (time-to-live) of 10 seconds:
258
- const memoryCache = await caching('memory', {
259
- max: 100,
260
- ttl: 10 * 1000 /*milliseconds*/
261
- });
291
+ await cache.wrap('key', () => 1, 5000, 3000)
292
+ // call function then save the result to cache
293
+ // => 1
262
294
 
263
- const app = express();
264
- const port = 3000;
265
-
266
- // A middleware function is defined to check the cache before processing the request. If the response is found in the cache, it is returned immediately. If not, the request proceeds to the route handler, and the response is cached before being sent:
267
- const cacheMiddleware = async (req, res, next) => {
268
- const key = req.originalUrl;
269
-
270
- try {
271
- const cachedResponse = await memoryCache.get(key);
272
- if (cachedResponse) {
273
- // Cache hit, return the cached response
274
- return res.send(cachedResponse);
275
- } else {
276
- // Cache miss, proceed to the route handler
277
- res.sendResponse = res.send;
278
- res.send = async (body) => {
279
- // Store the response in cache
280
- await memoryCache.set(key, body);
281
- res.sendResponse(body);
282
- };
283
- next();
284
- }
285
- } catch (err) {
286
- next(err);
287
- }
288
- };
289
-
290
- // The cacheMiddleware is applied to the /data route, which simulates a slow database call with a 2-second delay:
291
- app.get('/data', cacheMiddleware, (req, res) => {
292
- // Simulate a slow database call
293
- setTimeout(() => {
294
- res.send({ data: 'This is some data', timestamp: new Date() });
295
- }, 2000); // 2 seconds delay
296
- });
295
+ await cache.wrap('key', () => 2, 5000, 3000)
296
+ // return data from cache, function will not be called again
297
+ // => 1
297
298
 
298
- app.listen(port, () => {
299
- console.log(`Server is running on http://localhost:${port}`);
300
- });
299
+ // wait 3 seconds
300
+ await sleep(3000)
301
+
302
+ await cache.wrap('key', () => 2, 5000, 3000)
303
+ // return data from cache, call function in background and save the result to cache
304
+ // => 1
305
+
306
+ await cache.wrap('key', () => 3, 5000, 3000)
307
+ // return data from cache, function will not be called
308
+ // => 2
301
309
 
310
+ await cache.wrap('error', () => {
311
+ throw new Error('failed')
312
+ })
313
+ // => error
302
314
  ```
315
+ **NOTES:**
303
316
 
304
- ## Store Engines
317
+ * The store that will be checked for refresh is the one where the key will be found first (highest priority).
318
+ * If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values.
319
+ * If no `ttl` is set for the key, the refresh mechanism will not be triggered.
305
320
 
306
- ### Official and updated to last version
321
+ See unit tests in [`test/wrap.test.ts`](./test/wrap.test.ts) for more information.
307
322
 
308
- - [node-cache-manager-redis-yet](https://github.com/jaredwray/cacheable/packages/cache-manager-redis-yet) (uses [node_redis](https://github.com/NodeRedis/node_redis))
323
+ ## Events
324
+ ### set
325
+ Fired when a key has been added or changed.
309
326
 
310
- - [node-cache-manager-ioredis-yet](https://github.com/jaredwray/cacheable/packages/cache-manager-ioredis-yet) (uses [ioredis](https://github.com/luin/ioredis))
327
+ ```ts
328
+ cache.on('set', ({ key, value, error }) => {
329
+ // ... do something ...
330
+ })
331
+ ```
311
332
 
312
- ### Third party
333
+ ### del
334
+ Fired when a key has been removed manually.
313
335
 
314
- - [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis) (uses [sol-redis-pool](https://github.com/joshuah/sol-redis-pool))
336
+ ```ts
337
+ cache.on('del', ({ key, error }) => {
338
+ // ... do something ...
339
+ })
340
+ ```
315
341
 
316
- - [node-cache-manager-redis-store](https://github.com/dabroek/node-cache-manager-redis-store) (uses [node_redis](https://github.com/NodeRedis/node_redis))
342
+ ### clear
343
+ Fired when the cache has been flushed.
317
344
 
318
- - [node-cache-manager-ioredis](https://github.com/Tirke/node-cache-manager-ioredis) (uses [ioredis](https://github.com/luin/ioredis))
345
+ ```ts
346
+ cache.on('clear', (error) => {
347
+ if (error) {
348
+ // ... do something ...
349
+ }
350
+ })
351
+ ```
319
352
 
320
- - [node-cache-manager-mongodb](https://github.com/v4l3r10/node-cache-manager-mongodb)
353
+ ### refresh
354
+ Fired when the cache has been refreshed in the background.
321
355
 
322
- - [node-cache-manager-mongoose](https://github.com/disjunction/node-cache-manager-mongoose)
356
+ ```ts
357
+ cache.on('refresh', ({ key, value, error }) => {
358
+ if (error) {
359
+ // ... do something ...
360
+ }
361
+ })
362
+ ```
323
363
 
324
- - [node-cache-manager-fs-binary](https://github.com/sheershoff/node-cache-manager-fs-binary)
364
+ See unit tests in [`test/events.test.ts`](./test/events.test.ts) for more information.
325
365
 
326
- - [node-cache-manager-fs-hash](https://github.com/rolandstarke/node-cache-manager-fs-hash)
366
+ ## Update on `redis` and `ioredis` Support
327
367
 
328
- - [node-cache-manager-hazelcast](https://github.com/marudor/node-cache-manager-hazelcast)
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`. [Keyv](https://keyv.org/) has a the storage adapter `@keyv/redis` which is a more supported and flexible storage adapter.
329
369
 
330
- - [node-cache-manager-memcached-store](https://github.com/theogravity/node-cache-manager-memcached-store)
370
+ ## Using Legacy Storage Adapters
331
371
 
332
- - [node-cache-manager-memory-store](https://github.com/theogravity/node-cache-manager-memory-store)
372
+ 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`:
333
373
 
334
- - [node-cache-manager-couchbase](https://github.com/davidepellegatta/node-cache-manager-couchbase)
374
+ ```ts
375
+ import { createCache, KeyvAdapter } from 'cache-manager';
376
+ import { Keyv } from 'keyv';
377
+ import { redisStore } from 'cache-manager-redis-yet';
335
378
 
336
- - [node-cache-manager-sqlite](https://github.com/maxpert/node-cache-manager-sqlite)
379
+ const adapter = new KeyvAdapter( await redisStore() );
380
+ const cache = createCache({
381
+ stores: [new Keyv({ store: adapter })],
382
+ });
383
+ ```
337
384
 
338
- - [@resolid/cache-manager-sqlite](https://github.com/huijiewei/cache-manager-sqlite) (uses [better-sqlite3](https://github.com/WiseLibs/better-sqlite3))
385
+ This adapter will allow you to add in any storage adapter. If there are issues it needs to follow `CacheManagerStore` interface.
339
386
 
340
387
  ## Contribute
341
388
 
342
- If you would like to contribute to the project, please read how to contribute here [CONTRIBUTING.md](./CONTRIBUTING.md).
389
+ 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).
343
390
 
344
391
  ## License
345
392
 
346
- cache-manager is licensed under the [MIT license](./LICENSE).
393
+ [MIT © Jared Wray ](./LICENSE)