From 7a67e0cb7309a401f9137bf2785c333bc45b831a Mon Sep 17 00:00:00 2001 From: Nate Silva Date: Thu, 17 Jun 2021 14:38:13 -0700 Subject: [PATCH] Use the specified TTL with external Redis instances --- index.js | 8 ++++++++ test/index.test.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/index.js b/index.js index 09ee778..419e307 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,14 @@ const redisStore = (...args) => { const storeArgs = redisCache.options; + if ( + storeArgs.ttl === undefined && + args.length > 0 && + typeof args[0].ttl === 'number' + ) { + storeArgs.ttl = args[0].ttl; + } + let self = { name: 'redis', isCacheableValue: storeArgs.isCacheableValue || (value => value !== undefined && value !== null), diff --git a/test/index.test.js b/test/index.test.js index 253cedb..4a0d6b9 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -341,6 +341,49 @@ describe('ttl', () => { done(); }); }); + + it('should retrieve a ttl when an external instance is used', (done) => { + const externalRedisInstanceCache = cacheManager.caching({ + store: redisStore, + redisInstance: new Redis({ + host: config.host, + port: config.port, + password: config.password, + db: config.db, + }), + ttl: config.ttl + }); + + externalRedisInstanceCache.set('foo', 'bar', () => { + redisCache.ttl('foo', (err, ttl) => { + expect(err).toEqual(null); + expect(ttl).toEqual(config.ttl); + done(); + }); + }) + }); + + it('should use the Redis ttl if both a global and Redis ttl are specified', (done) => { + const externalRedisInstanceCache = cacheManager.caching({ + store: redisStore, + redisInstance: new Redis({ + host: config.host, + port: config.port, + password: config.password, + db: config.db, + ttl: config.ttl * 99 + }), + ttl: config.ttl + }); + + externalRedisInstanceCache.set('foo', 'bar', () => { + redisCache.ttl('foo', (err, ttl) => { + expect(err).toEqual(null); + expect(ttl).toEqual(config.ttl * 99); + done(); + }); + }) + }); }); describe('keys', () => {