cache-manager
Advanced tools
Comparing version 5.5.3 to 5.6.0
@@ -1,16 +0,10 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createCache = exports.caching = void 0; | ||
const eventemitter3_1 = __importDefault(require("eventemitter3")); | ||
const promise_coalesce_1 = require("promise-coalesce"); | ||
const index_js_1 = require("./stores/index.js"); | ||
import EventEmitter from 'eventemitter3'; | ||
import { coalesceAsync } from 'promise-coalesce'; | ||
import { memoryStore, } from './stores/index.js'; | ||
/** | ||
* Generic caching interface that wraps any caching library with a compatible interface. | ||
*/ | ||
async function caching(factory, arguments_) { | ||
export async function caching(factory, arguments_) { | ||
if (factory === 'memory') { | ||
const store = (0, index_js_1.memoryStore)(arguments_); | ||
const store = memoryStore(arguments_); | ||
return createCache(store, arguments_); | ||
@@ -25,8 +19,7 @@ } | ||
} | ||
exports.caching = caching; | ||
/** | ||
* Create cache instance by store (non-async). | ||
*/ | ||
function createCache(store, arguments_) { | ||
const eventEmitter = new eventemitter3_1.default(); | ||
export function createCache(store, arguments_) { | ||
const eventEmitter = new EventEmitter(); | ||
return { | ||
@@ -44,3 +37,3 @@ /** | ||
const refreshThresholdConfig = refreshThreshold ?? arguments_?.refreshThreshold ?? 0; | ||
return (0, promise_coalesce_1.coalesceAsync)(key, async () => { | ||
return coalesceAsync(key, async () => { | ||
const value = await store.get(key).catch(error => { | ||
@@ -65,3 +58,3 @@ const errorEvent = { error, key, operation: 'wrap' }; | ||
if (remainingTtl !== -1 && remainingTtl < refreshThresholdConfig) { | ||
(0, promise_coalesce_1.coalesceAsync)(`+++${key}`, function_) | ||
coalesceAsync(`+++${key}`, function_) | ||
.then(async (result) => store.set(key, result, cacheTtl)) | ||
@@ -96,3 +89,2 @@ .catch(async (error) => { | ||
} | ||
exports.createCache = createCache; | ||
//# sourceMappingURL=caching.js.map |
@@ -1,20 +0,4 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./caching.js"), exports); | ||
__exportStar(require("./multi-caching.js"), exports); | ||
__exportStar(require("./stores/index.js"), exports); | ||
export * from './caching.js'; | ||
export * from './multi-caching.js'; | ||
export * from './stores/index.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,13 +0,7 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.multiCaching = void 0; | ||
const eventemitter3_1 = __importDefault(require("eventemitter3")); | ||
import EventEmitter from 'eventemitter3'; | ||
/** | ||
* Module that lets you specify a hierarchy of caches. | ||
*/ | ||
function multiCaching(caches) { | ||
const eventEmitter = new eventemitter3_1.default(); | ||
export function multiCaching(caches) { | ||
const eventEmitter = new EventEmitter(); | ||
for (const cache of caches) { | ||
@@ -129,3 +123,2 @@ cache.on('error', event => eventEmitter.emit('error', event)); | ||
} | ||
exports.multiCaching = multiCaching; | ||
//# sourceMappingURL=multi-caching.js.map |
@@ -1,18 +0,2 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./memory.js"), exports); | ||
export * from './memory.js'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,12 +0,6 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.memoryStore = void 0; | ||
const lru_cache_1 = require("lru-cache"); | ||
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep")); | ||
import { LRUCache } from 'lru-cache'; | ||
import cloneDeep from 'lodash.clonedeep'; | ||
function clone(object) { | ||
if (typeof object === 'object' && object !== null) { | ||
return (0, lodash_clonedeep_1.default)(object); | ||
return cloneDeep(object); | ||
} | ||
@@ -18,3 +12,3 @@ return object; | ||
*/ | ||
function memoryStore(arguments_) { | ||
export function memoryStore(arguments_) { | ||
const shouldCloneBeforeSet = arguments_?.shouldCloneBeforeSet !== false; // Clone by default | ||
@@ -28,3 +22,3 @@ const isCacheable = arguments_?.isCacheable ?? (value => value !== undefined); | ||
}; | ||
const lruCache = new lru_cache_1.LRUCache(lruOptions); | ||
const lruCache = new LRUCache(lruOptions); | ||
return { | ||
@@ -92,3 +86,2 @@ async del(key) { | ||
} | ||
exports.memoryStore = memoryStore; | ||
//# sourceMappingURL=memory.js.map |
{ | ||
"name": "cache-manager", | ||
"version": "5.5.3", | ||
"version": "5.6.0", | ||
"description": "Cache module for Node.js", | ||
@@ -11,2 +11,5 @@ "main": "dist/index.js", | ||
], | ||
"engines": { | ||
"node": ">= 18" | ||
}, | ||
"repository": { | ||
@@ -46,10 +49,10 @@ "type": "git", | ||
"@types/lodash.clonedeep": "^4.5.9", | ||
"@types/node": "^20.12.7", | ||
"@typescript-eslint/eslint-plugin": "^7.7.1", | ||
"@typescript-eslint/parser": "^7.7.1", | ||
"@vitest/coverage-v8": "^1.5.2", | ||
"@types/node": "^20.12.12", | ||
"@typescript-eslint/eslint-plugin": "^7.10.0", | ||
"@typescript-eslint/parser": "^7.10.0", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"eslint-config-xo-typescript": "^4.0.0", | ||
"rimraf": "^5.0.5", | ||
"rimraf": "^5.0.7", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.5.2", | ||
"vitest": "^1.6.0", | ||
"xo": "^0.58.0" | ||
@@ -56,0 +59,0 @@ }, |
@@ -21,2 +21,3 @@ # cache-manager | ||
* [Error Handling](#error-handling) | ||
* [Express Middleware](#express-middleware) | ||
* [Store Engines](#store-engines) | ||
@@ -91,4 +92,2 @@ * [Contribute](#contribute) | ||
#### [Example Express App Usage](./examples/express/src/index.mts) | ||
#### Custom Stores | ||
@@ -238,2 +237,58 @@ | ||
## Express Middleware | ||
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. | ||
```typescript | ||
// The code imports the necessary modules using ES module syntax: | ||
import { caching } from 'cache-manager'; | ||
import express from 'express'; | ||
// The memory cache is initialized using cache-manager with a maximum of 100 items and a TTL (time-to-live) of 10 seconds: | ||
const memoryCache = await caching('memory', { | ||
max: 100, | ||
ttl: 10 /* seconds */ | ||
}); | ||
const app = express(); | ||
const port = 3000; | ||
// 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: | ||
const cacheMiddleware = async (req, res, next) => { | ||
const key = req.originalUrl; | ||
try { | ||
const cachedResponse = await memoryCache.get(key); | ||
if (cachedResponse) { | ||
// Cache hit, return the cached response | ||
return res.send(cachedResponse); | ||
} else { | ||
// Cache miss, proceed to the route handler | ||
res.sendResponse = res.send; | ||
res.send = async (body) => { | ||
// Store the response in cache | ||
await memoryCache.set(key, body); | ||
res.sendResponse(body); | ||
}; | ||
next(); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
// The cacheMiddleware is applied to the /data route, which simulates a slow database call with a 2-second delay: | ||
app.get('/data', cacheMiddleware, (req, res) => { | ||
// Simulate a slow database call | ||
setTimeout(() => { | ||
res.send({ data: 'This is some data', timestamp: new Date() }); | ||
}, 2000); // 2 seconds delay | ||
}); | ||
app.listen(port, () => { | ||
console.log(`Server is running on http://localhost:${port}`); | ||
}); | ||
``` | ||
## Store Engines | ||
@@ -240,0 +295,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
334
30743
381