cache-manager 5.5.3 → 5.6.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 +57 -2
- package/package.json +10 -7
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ A cache module for nodejs that allows easy wrapping of functions in cache, tiere
|
|
|
18
18
|
* [Cache Manager Options](#cache-manager-options)
|
|
19
19
|
* [Refresh cache keys in background](#refresh-cache-keys-in-background)
|
|
20
20
|
* [Error Handling](#error-handling)
|
|
21
|
+
* [Express Middleware](#express-middleware)
|
|
21
22
|
* [Store Engines](#store-engines)
|
|
22
23
|
* [Contribute](#contribute)
|
|
23
24
|
* [License](#license)
|
|
@@ -87,8 +88,6 @@ console.log(await memoryCache.store.mget('foo', 'foo2'));
|
|
|
87
88
|
await memoryCache.store.mdel('foo', 'foo2');
|
|
88
89
|
```
|
|
89
90
|
|
|
90
|
-
#### [Example Express App Usage](./examples/express/src/index.mts)
|
|
91
|
-
|
|
92
91
|
#### Custom Stores
|
|
93
92
|
|
|
94
93
|
You can use your own custom store by creating one with the same API as the built-in memory stores.
|
|
@@ -233,6 +232,62 @@ multicache.on('error', (error) => {
|
|
|
233
232
|
});
|
|
234
233
|
```
|
|
235
234
|
|
|
235
|
+
## Express Middleware
|
|
236
|
+
|
|
237
|
+
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.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
// The code imports the necessary modules using ES module syntax:
|
|
241
|
+
import { caching } from 'cache-manager';
|
|
242
|
+
import express from 'express';
|
|
243
|
+
|
|
244
|
+
// The memory cache is initialized using cache-manager with a maximum of 100 items and a TTL (time-to-live) of 10 seconds:
|
|
245
|
+
const memoryCache = await caching('memory', {
|
|
246
|
+
max: 100,
|
|
247
|
+
ttl: 10 /* seconds */
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const app = express();
|
|
251
|
+
const port = 3000;
|
|
252
|
+
|
|
253
|
+
// 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:
|
|
254
|
+
const cacheMiddleware = async (req, res, next) => {
|
|
255
|
+
const key = req.originalUrl;
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const cachedResponse = await memoryCache.get(key);
|
|
259
|
+
if (cachedResponse) {
|
|
260
|
+
// Cache hit, return the cached response
|
|
261
|
+
return res.send(cachedResponse);
|
|
262
|
+
} else {
|
|
263
|
+
// Cache miss, proceed to the route handler
|
|
264
|
+
res.sendResponse = res.send;
|
|
265
|
+
res.send = async (body) => {
|
|
266
|
+
// Store the response in cache
|
|
267
|
+
await memoryCache.set(key, body);
|
|
268
|
+
res.sendResponse(body);
|
|
269
|
+
};
|
|
270
|
+
next();
|
|
271
|
+
}
|
|
272
|
+
} catch (err) {
|
|
273
|
+
next(err);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// The cacheMiddleware is applied to the /data route, which simulates a slow database call with a 2-second delay:
|
|
278
|
+
app.get('/data', cacheMiddleware, (req, res) => {
|
|
279
|
+
// Simulate a slow database call
|
|
280
|
+
setTimeout(() => {
|
|
281
|
+
res.send({ data: 'This is some data', timestamp: new Date() });
|
|
282
|
+
}, 2000); // 2 seconds delay
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
app.listen(port, () => {
|
|
286
|
+
console.log(`Server is running on http://localhost:${port}`);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
```
|
|
290
|
+
|
|
236
291
|
## Store Engines
|
|
237
292
|
|
|
238
293
|
### Official and updated to last version
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.6.1",
|
|
4
4
|
"description": "Cache module for Node.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"dist/**/*.js",
|
|
9
9
|
"dist/**/*.d.ts"
|
|
10
10
|
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">= 18"
|
|
13
|
+
},
|
|
11
14
|
"repository": {
|
|
12
15
|
"type": "git",
|
|
13
16
|
"url": "https://github.com/jaredwray/cache-manager.git"
|
|
@@ -42,14 +45,14 @@
|
|
|
42
45
|
"devDependencies": {
|
|
43
46
|
"@faker-js/faker": "^8.4.1",
|
|
44
47
|
"@types/lodash.clonedeep": "^4.5.9",
|
|
45
|
-
"@types/node": "^20.12.
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
47
|
-
"@typescript-eslint/parser": "^7.
|
|
48
|
-
"@vitest/coverage-v8": "^1.
|
|
48
|
+
"@types/node": "^20.12.12",
|
|
49
|
+
"@typescript-eslint/eslint-plugin": "^7.10.0",
|
|
50
|
+
"@typescript-eslint/parser": "^7.10.0",
|
|
51
|
+
"@vitest/coverage-v8": "^1.6.0",
|
|
49
52
|
"eslint-config-xo-typescript": "^4.0.0",
|
|
50
|
-
"rimraf": "^5.0.
|
|
53
|
+
"rimraf": "^5.0.7",
|
|
51
54
|
"typescript": "^5.4.5",
|
|
52
|
-
"vitest": "^1.
|
|
55
|
+
"vitest": "^1.6.0",
|
|
53
56
|
"xo": "^0.58.0"
|
|
54
57
|
},
|
|
55
58
|
"xo": {
|