From 39e62f21cc4a5edd44396eea37d30fb23d8b3285 Mon Sep 17 00:00:00 2001 From: Toby Hinloopen Date: Fri, 9 Oct 2020 19:40:57 +0200 Subject: [PATCH] Allow custom redis client --- README.md | 21 ++++++++++++++++++--- index.js | 6 +++--- package-lock.json | 2 +- test/index.test.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 77b37fc..bc0b32f 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ Redis store for node cache manager ================================== -Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager). +Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager). How is this package different from `node-cache-manager-redis`? ---------------------------------------------------------------------------------- This is a **completely different version** than the earlier [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis). This package does not use `redis-pool` which is unnecessary and not actively maintained. - + This package aims to provide **the most simple wrapper possible** by just passing the configuration to the underlying `node_redis` package. Installation @@ -141,6 +141,21 @@ multiCache.wrap(key2, (cb) => { }); ``` +### Existing Redis client + +```js +var cacheManager = require('cache-manager'); +var redisStore = require('cache-manager-redis-store'); +var redis = require('redis'); +var redisClient = redis.createClient(); + +var redisCache = cacheManager.caching({ + store: redisStore, + client: redisClient, + ttl: 600 +}); +``` + Contribution ------------ @@ -150,4 +165,4 @@ Want to help improve this package? We take [pull requests](https://github.com/da License ------- -The `node-cache-manager-redis-store` is licensed under the MIT license. \ No newline at end of file +The `node-cache-manager-redis-store` is licensed under the MIT license. diff --git a/index.js b/index.js index d727e30..9af8508 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ import Redis from 'redis'; const redisStore = (...args) => { - const redisCache = Redis.createClient(...args); - const storeArgs = redisCache.options; + const redisCache = args[0].client ? args[0].client : Redis.createClient(...args); + const storeArgs = { ...args[0], ...redisCache.options }; return { name: 'redis', @@ -148,7 +148,7 @@ const redisStore = (...args) => { if (!cb) { cb = (err, result) => (err ? reject(err) : resolve(result)); } - + redisCache.flushdb(handleResponse(cb)); }) ), diff --git a/package-lock.json b/package-lock.json index ded229b..c186eac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cache-manager-redis-store", - "version": "1.5.0", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/test/index.test.js b/test/index.test.js index 6dfa85e..86329c2 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,6 @@ import cacheManager from 'cache-manager'; import redisStore from '../index'; +import redis from 'redis'; let redisCache; let customRedisCache; @@ -308,7 +309,7 @@ describe('get', () => { it('should reject promise on error', (done) => { const client = redisCache.store.getClient(); client.get = (key, cb) => cb(new Error('Something went wrong')); - + redisCache.get('foo') .catch((err) => { expect(err.message).toEqual('Something went wrong'); @@ -693,6 +694,32 @@ describe('wrap function', () => { }); } + it('should work with manual client', () => { + var customClientCache = cacheManager.caching({ + store: redisStore, + client: redis.createClient(config), + }); + + const userId = 123; + + // First call to wrap should run the code + return customClientCache + .wrap( + 'wrap-promise', + () => getUserPromise(userId), + ) + .then((user) => { + expect(user.id).toEqual(userId); + + // Second call to wrap should retrieve from cache + return customClientCache.wrap( + 'wrap-promise', + () => getUserPromise(userId + 1), + ) + .then((user) => expect(user.id).toEqual(userId)); + }); + }) + it('should be able to cache objects', (done) => { const userId = 123;