Skip to content

Commit

Permalink
Supports ttl constructor option when constructed with an external red…
Browse files Browse the repository at this point in the history
…is instance
  • Loading branch information
wbk committed Feb 3, 2021
1 parent 3bf4514 commit 85eb75d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const redisStore = (...args) => {
redisCache = new Redis(...args);
}

const storeArgs = redisCache.options;
const storeArgs = {
ttl: args.length > 0 && args[0].ttl,
...redisCache.options
};

let self = {
name: 'redis',
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const redisStore = (...args) => {
redisCache = new Redis(...args);
}

const storeArgs = redisCache.options;
const storeArgs = {
ttl: args.length > 0 && args[0].ttl,
...redisCache.options
};

let self = {
name: 'redis',
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 85eb75d

Please sign in to comment.