Skip to content

Commit 85eb75d

Browse files
committed
Supports ttl constructor option when constructed with an external redis instance
1 parent 3bf4514 commit 85eb75d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

dist/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const redisStore = (...args) => {
1818
redisCache = new Redis(...args);
1919
}
2020

21-
const storeArgs = redisCache.options;
21+
const storeArgs = {
22+
ttl: args.length > 0 && args[0].ttl,
23+
...redisCache.options
24+
};
2225

2326
let self = {
2427
name: 'redis',

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ const redisStore = (...args) => {
1616
redisCache = new Redis(...args);
1717
}
1818

19-
const storeArgs = redisCache.options;
19+
const storeArgs = {
20+
ttl: args.length > 0 && args[0].ttl,
21+
...redisCache.options
22+
};
2023

2124
let self = {
2225
name: 'redis',

test/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ describe('set', () => {
132132
});
133133
});
134134

135+
it('should respect the ttl option when supplied an external redis instance', (done) => {
136+
const externalRedisInstanceCache = cacheManager.caching({
137+
store: redisStore,
138+
redisInstance: new Redis({
139+
host: config.host,
140+
port: config.port,
141+
password: config.password,
142+
db: config.db,
143+
}),
144+
ttl: 123
145+
});
146+
147+
externalRedisInstanceCache.set('foo', 'bar', (err) => {
148+
expect(err).toEqual(null);
149+
redisCache.ttl('foo', (err, ttl) => {
150+
expect(err).toEqual(null);
151+
expect(ttl).toEqual(123);
152+
done();
153+
});
154+
});
155+
});
156+
135157
it('should not be able to store a null value (not cacheable)', (done) => {
136158
redisCache.set('foo2', null, (err) => {
137159
if (err) {

0 commit comments

Comments
 (0)