cache-manager
Advanced tools
Comparing version
@@ -73,3 +73,3 @@ "use strict"; | ||
*/ | ||
wrap: function (key, fn) { return __awaiter(_this, void 0, void 0, function () { | ||
wrap: function (key, fn, ttl) { return __awaiter(_this, void 0, void 0, function () { | ||
var value, result; | ||
@@ -85,3 +85,3 @@ return __generator(this, function (_a) { | ||
result = _a.sent(); | ||
return [4 /*yield*/, store.set(key, result)]; | ||
return [4 /*yield*/, store.set(key, result, ttl)]; | ||
case 3: | ||
@@ -88,0 +88,0 @@ _a.sent(); |
{ | ||
"name": "cache-manager", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"description": "Cache module for Node.js", | ||
@@ -38,9 +38,9 @@ "main": "dist/index.js", | ||
"@commitlint/config-conventional": "17.1.0", | ||
"@faker-js/faker": "7.5.0", | ||
"@faker-js/faker": "7.6.0", | ||
"@release-it/conventional-changelog": "5.1.1", | ||
"@types/lodash.clonedeep": "4.5.7", | ||
"@types/node": "18.8.3", | ||
"@typescript-eslint/eslint-plugin": "5.39.0", | ||
"@typescript-eslint/parser": "5.39.0", | ||
"@vitest/coverage-c8": "0.24.0", | ||
"@typescript-eslint/eslint-plugin": "5.40.1", | ||
"@typescript-eslint/parser": "5.40.1", | ||
"@vitest/coverage-c8": "0.24.3", | ||
"dotenv-cli": "6.0.0", | ||
@@ -55,6 +55,6 @@ "eslint": "8.25.0", | ||
"typescript": "4.8.4", | ||
"vitest": "0.24.0" | ||
"vitest": "0.24.3" | ||
}, | ||
"lint-staged": { | ||
"*.{ts,js}": "eslint --cache --fix", | ||
"*.{ts,js,mts,mjs}": "eslint --cache --fix", | ||
"*.{md,json,yml}": "prettier --write" | ||
@@ -68,7 +68,7 @@ }, | ||
"check": "pnpm lint:check && pnpm fmt:check", | ||
"lint": "eslint --cache --max-warnings 0 --fix '**/*.ts'", | ||
"lint": "eslint --cache --max-warnings 0 --fix '**/*.{ts,mts,js}'", | ||
"fmt": "prettier --write '**/*.{md,json,yml}'", | ||
"lint:check": "eslint --cache --max-warnings 0 '**/*.ts'", | ||
"lint:check": "eslint --cache --max-warnings 0 '**/*.{ts,mts,js}'", | ||
"fmt:check": "prettier --check '**/*.{md,json,yml}'" | ||
} | ||
} |
@@ -9,2 +9,3 @@ # node-cache-manager [](https://www.npmjs.com/package/cache-manager) [](https://codecov.io/gh/node-cache-manager/node-cache-manager) | ||
- Made with Typescript and compatible with [ESModules](https://nodejs.org/docs/latest-v14.x/api/esm.html) | ||
- Easy way to wrap any function in cache. | ||
@@ -16,7 +17,2 @@ - Tiered caches -- data gets stored in each cache and fetched from the highest. | ||
## Express.js Example | ||
See the [Express.js cache-manager example app](https://github.com/BryanDonovan/node-cache-manager-express-example) to see how to use | ||
`node-cache-manager` in your applications. | ||
## Installation | ||
@@ -26,2 +22,83 @@ | ||
## Usage Examples | ||
### Single Store | ||
```typescript | ||
import { caching } from 'cache-manager'; | ||
const memoryCache = await caching('memory', { | ||
max: 100, | ||
ttl: 10 * 1000 /*miliseconds*/, | ||
}); | ||
const ttl = 5 * 1000; /*miliseconds*/ | ||
await memoryCache.set('foo', 'bar', ttl); | ||
console.log(await memoryCache.get('foo')); | ||
// >> "bar" | ||
await memoryCache.del('foo'); | ||
console.log(await memoryCache.get('foo')); | ||
// >> undefined | ||
const getUser = (id: string) => new Promise.resolve({ id: id, name: 'Bob' }); | ||
const userId = 123; | ||
const key = 'user_' + userId; | ||
console.log(await memoryCache.wrap(key, () => getUser(userId), ttl)); | ||
// >> { id: 123, name: 'Bob' } | ||
``` | ||
See unit tests in `test/caching.ts` for more information. | ||
#### Example setting/getting several keys with mset() and mget() | ||
```typescript | ||
await memoryCache.mset( | ||
[ | ||
['foo', 'bar'], | ||
['foo2', 'bar2'], | ||
], | ||
ttl, | ||
); | ||
console.log(await memoryCache.mget('foo', 'foo2')); | ||
// >> ['bar', 'bar2'] | ||
// Delete keys with mdel() passing arguments... | ||
await memoryCache.mdel('foo', 'foo2'); | ||
``` | ||
#### [Example Express App Usage](./examples/express/src/index.mts) | ||
#### Custom Stores | ||
You can use your own custom store by creating one with the same API as the | ||
built-in memory stores (such as a redis or memcached store). To use your own store just pass | ||
in an instance of it. [example](https://github.com/node-cache-manager/node-cache-manager-redis-yet) | ||
### Multi-Store | ||
```typescript | ||
import { multiCaching } from 'cache-manager'; | ||
const multiCache = await multiCaching([memoryCache, someOtherCache]); | ||
const userId2 = 456; | ||
const key2 = 'user_' + userId; | ||
const ttl = 5; | ||
// Sets in all caches. | ||
await multiCache.set('foo2', 'bar2', ttl); | ||
// Fetches from highest priority cache that has the key. | ||
console.log(await multiCache.get('foo2')); | ||
// >> "bar2" | ||
// Delete from all caches | ||
await multiCache.del('foo2'); | ||
``` | ||
## Store Engines | ||
@@ -28,0 +105,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
67491
2.78%137
128.33%