Skip to content

Commit 5cbf593

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

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

dist/index.js

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

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

2327
let self = {
2428
name: 'redis',

index.js

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

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

2125
let self = {
2226
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)