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 5cbf593
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion dist/index.js
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -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',
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
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 5cbf593

Please sign in to comment.