Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the specified TTL with external Redis instances #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const redisStore = (...args) => {

const storeArgs = redisCache.options;

if (
storeArgs.ttl === undefined &&
args.length > 0 &&
typeof args[0].ttl === 'number'
) {
storeArgs.ttl = args[0].ttl;
}

let self = {
name: 'redis',
isCacheableValue: storeArgs.isCacheableValue || (value => value !== undefined && value !== null),
Expand Down
43 changes: 43 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,49 @@ describe('ttl', () => {
done();
});
});

it('should retrieve a ttl when an external instance is used', (done) => {
const externalRedisInstanceCache = cacheManager.caching({
store: redisStore,
redisInstance: new Redis({
host: config.host,
port: config.port,
password: config.password,
db: config.db,
}),
ttl: config.ttl
});

externalRedisInstanceCache.set('foo', 'bar', () => {
redisCache.ttl('foo', (err, ttl) => {
expect(err).toEqual(null);
expect(ttl).toEqual(config.ttl);
done();
});
})
});

it('should use the Redis ttl if both a global and Redis ttl are specified', (done) => {
const externalRedisInstanceCache = cacheManager.caching({
store: redisStore,
redisInstance: new Redis({
host: config.host,
port: config.port,
password: config.password,
db: config.db,
ttl: config.ttl * 99
}),
ttl: config.ttl
});

externalRedisInstanceCache.set('foo', 'bar', () => {
redisCache.ttl('foo', (err, ttl) => {
expect(err).toEqual(null);
expect(ttl).toEqual(config.ttl * 99);
done();
});
})
});
});

describe('keys', () => {
Expand Down