From 5cbf5933aa265a345c63897fd7c3c62119403b7d Mon Sep 17 00:00:00 2001
From: Walter Kalata <ping@walter.dev>
Date: Tue, 2 Feb 2021 22:31:16 -0700
Subject: [PATCH] Supports ttl constructor option when constructed with an
 external redis instance

---
 dist/index.js      |  6 +++++-
 index.js           |  6 +++++-
 test/index.test.js | 22 ++++++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index 9fea7dc..01752b8 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -18,7 +18,11 @@ const redisStore = (...args) => {
     redisCache = new Redis(...args);
   }
 
-  const storeArgs = redisCache.options;
+  const storeArgs = {
+    ...redisCache.options,
+    ttl: args.length > 0 ? args[0].ttl : undefined
+  };
+
 
   let self = {
     name: 'redis',
diff --git a/index.js b/index.js
index 09ee778..5e109b3 100644
--- a/index.js
+++ b/index.js
@@ -16,7 +16,11 @@ const redisStore = (...args) => {
     redisCache = new Redis(...args);
   }
 
-  const storeArgs = redisCache.options;
+  const storeArgs = {
+    ...redisCache.options,
+    ttl: args.length > 0 ? args[0].ttl : undefined
+  };
+
 
   let self = {
     name: 'redis',
diff --git a/test/index.test.js b/test/index.test.js
index 253cedb..8ea650e 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -132,6 +132,28 @@ describe('set', () => {
     });
   });
 
+  it('should respect the ttl option when supplied an external redis instance', (done) => {
+    const externalRedisInstanceCache = cacheManager.caching({
+      store: redisStore,
+      redisInstance: new Redis({
+        host: config.host,
+        port: config.port,
+        password: config.password,
+        db: config.db,
+      }),
+      ttl: 123
+    });
+
+    externalRedisInstanceCache.set('foo', 'bar', (err) => {
+      expect(err).toEqual(null);
+      redisCache.ttl('foo', (err, ttl) => {
+        expect(err).toEqual(null);
+        expect(ttl).toEqual(123);
+        done();
+      });
+    });
+  });
+
   it('should not be able to store a null value (not cacheable)', (done) => {
     redisCache.set('foo2', null, (err) => {
       if (err) {