cache-manager 5.5.3 → 5.6.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 +57 -2
- package/dist/caching.js +9 -17
- package/dist/index.js +3 -19
- package/dist/multi-caching.js +3 -10
- package/dist/stores/index.js +1 -17
- package/dist/stores/memory.js +5 -12
- 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/dist/caching.js
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createCache = exports.caching = void 0;
|
|
7
|
-
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
8
|
-
const promise_coalesce_1 = require("promise-coalesce");
|
|
9
|
-
const index_js_1 = require("./stores/index.js");
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
2
|
+
import { coalesceAsync } from 'promise-coalesce';
|
|
3
|
+
import { memoryStore, } from './stores/index.js';
|
|
10
4
|
/**
|
|
11
5
|
* Generic caching interface that wraps any caching library with a compatible interface.
|
|
12
6
|
*/
|
|
13
|
-
async function caching(factory, arguments_) {
|
|
7
|
+
export async function caching(factory, arguments_) {
|
|
14
8
|
if (factory === 'memory') {
|
|
15
|
-
const store =
|
|
9
|
+
const store = memoryStore(arguments_);
|
|
16
10
|
return createCache(store, arguments_);
|
|
17
11
|
}
|
|
18
12
|
if (typeof factory === 'function') {
|
|
@@ -22,12 +16,11 @@ async function caching(factory, arguments_) {
|
|
|
22
16
|
const store = factory;
|
|
23
17
|
return createCache(store, arguments_);
|
|
24
18
|
}
|
|
25
|
-
exports.caching = caching;
|
|
26
19
|
/**
|
|
27
20
|
* Create cache instance by store (non-async).
|
|
28
21
|
*/
|
|
29
|
-
function createCache(store, arguments_) {
|
|
30
|
-
const eventEmitter = new
|
|
22
|
+
export function createCache(store, arguments_) {
|
|
23
|
+
const eventEmitter = new EventEmitter();
|
|
31
24
|
return {
|
|
32
25
|
/**
|
|
33
26
|
* Wraps a function in cache. I.e., the first time the function is run,
|
|
@@ -40,7 +33,7 @@ function createCache(store, arguments_) {
|
|
|
40
33
|
*/
|
|
41
34
|
async wrap(key, function_, ttl, refreshThreshold) {
|
|
42
35
|
const refreshThresholdConfig = refreshThreshold ?? arguments_?.refreshThreshold ?? 0;
|
|
43
|
-
return
|
|
36
|
+
return coalesceAsync(key, async () => {
|
|
44
37
|
const value = await store.get(key).catch(error => {
|
|
45
38
|
const errorEvent = { error, key, operation: 'wrap' };
|
|
46
39
|
eventEmitter.emit('error', errorEvent);
|
|
@@ -60,7 +53,7 @@ function createCache(store, arguments_) {
|
|
|
60
53
|
const cacheTtl = typeof ttl === 'function' ? ttl(value) : ttl;
|
|
61
54
|
const remainingTtl = await store.ttl(key);
|
|
62
55
|
if (remainingTtl !== -1 && remainingTtl < refreshThresholdConfig) {
|
|
63
|
-
|
|
56
|
+
coalesceAsync(`+++${key}`, function_)
|
|
64
57
|
.then(async (result) => store.set(key, result, cacheTtl))
|
|
65
58
|
.catch(async (error) => {
|
|
66
59
|
const errorEvent = {
|
|
@@ -90,5 +83,4 @@ function createCache(store, arguments_) {
|
|
|
90
83
|
removeListener: (event, handler) => eventEmitter.removeListener(event, handler),
|
|
91
84
|
};
|
|
92
85
|
}
|
|
93
|
-
exports.createCache = createCache;
|
|
94
86
|
//# sourceMappingURL=caching.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./caching.js"), exports);
|
|
18
|
-
__exportStar(require("./multi-caching.js"), exports);
|
|
19
|
-
__exportStar(require("./stores/index.js"), exports);
|
|
1
|
+
export * from './caching.js';
|
|
2
|
+
export * from './multi-caching.js';
|
|
3
|
+
export * from './stores/index.js';
|
|
20
4
|
//# sourceMappingURL=index.js.map
|
package/dist/multi-caching.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.multiCaching = void 0;
|
|
7
|
-
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
1
|
+
import EventEmitter from 'eventemitter3';
|
|
8
2
|
/**
|
|
9
3
|
* Module that lets you specify a hierarchy of caches.
|
|
10
4
|
*/
|
|
11
|
-
function multiCaching(caches) {
|
|
12
|
-
const eventEmitter = new
|
|
5
|
+
export function multiCaching(caches) {
|
|
6
|
+
const eventEmitter = new EventEmitter();
|
|
13
7
|
for (const cache of caches) {
|
|
14
8
|
cache.on('error', event => eventEmitter.emit('error', event));
|
|
15
9
|
}
|
|
@@ -126,5 +120,4 @@ function multiCaching(caches) {
|
|
|
126
120
|
removeListener: (event, handler) => eventEmitter.removeListener(event, handler),
|
|
127
121
|
};
|
|
128
122
|
}
|
|
129
|
-
exports.multiCaching = multiCaching;
|
|
130
123
|
//# sourceMappingURL=multi-caching.js.map
|
package/dist/stores/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./memory.js"), exports);
|
|
1
|
+
export * from './memory.js';
|
|
18
2
|
//# sourceMappingURL=index.js.map
|
package/dist/stores/memory.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.memoryStore = void 0;
|
|
7
|
-
const lru_cache_1 = require("lru-cache");
|
|
8
|
-
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
import cloneDeep from 'lodash.clonedeep';
|
|
9
3
|
function clone(object) {
|
|
10
4
|
if (typeof object === 'object' && object !== null) {
|
|
11
|
-
return (
|
|
5
|
+
return cloneDeep(object);
|
|
12
6
|
}
|
|
13
7
|
return object;
|
|
14
8
|
}
|
|
15
9
|
/**
|
|
16
10
|
* Wrapper for lru-cache.
|
|
17
11
|
*/
|
|
18
|
-
function memoryStore(arguments_) {
|
|
12
|
+
export function memoryStore(arguments_) {
|
|
19
13
|
const shouldCloneBeforeSet = arguments_?.shouldCloneBeforeSet !== false; // Clone by default
|
|
20
14
|
const isCacheable = arguments_?.isCacheable ?? (value => value !== undefined);
|
|
21
15
|
const lruOptions = {
|
|
@@ -24,7 +18,7 @@ function memoryStore(arguments_) {
|
|
|
24
18
|
max: arguments_?.max ?? 500,
|
|
25
19
|
ttl: arguments_?.ttl === undefined ? 0 : arguments_.ttl,
|
|
26
20
|
};
|
|
27
|
-
const lruCache = new
|
|
21
|
+
const lruCache = new LRUCache(lruOptions);
|
|
28
22
|
return {
|
|
29
23
|
async del(key) {
|
|
30
24
|
lruCache.delete(key);
|
|
@@ -87,5 +81,4 @@ function memoryStore(arguments_) {
|
|
|
87
81
|
},
|
|
88
82
|
};
|
|
89
83
|
}
|
|
90
|
-
exports.memoryStore = memoryStore;
|
|
91
84
|
//# sourceMappingURL=memory.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.6.0",
|
|
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": {
|