Skip to content

Commit 801308d

Browse files
authored
RedisCacheAdapter: add some additional checks and defaults (#2991)
1 parent af700d7 commit 801308d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Adapters/Cache/RedisCacheAdapter.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import redis from 'redis';
22
import logger from '../../logger';
33

4-
const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds
4+
const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds in milliseconds
55

66
function debug() {
77
logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]);
@@ -30,17 +30,23 @@ export class RedisCacheAdapter {
3030
return this.p;
3131
}
3232

33-
put(key, value, ttl) {
33+
put(key, value, ttl = DEFAULT_REDIS_TTL) {
3434
value = JSON.stringify(value);
3535
debug('put', key, value, ttl);
36+
if (ttl === 0) {
37+
return this.p; // ttl of zero is a logical no-op, but redis cannot set expire time of zero
38+
}
39+
if (ttl < 0 || isNaN(ttl)) {
40+
ttl = DEFAULT_REDIS_TTL;
41+
}
3642
this.p = this.p.then(() => {
3743
return new Promise((resolve, _) => {
38-
if (ttl) {
39-
this.client.psetex(key, ttl, value, function(err, res) {
44+
if (ttl === Infinity) {
45+
this.client.set(key, value, function(err, res) {
4046
resolve();
4147
});
4248
} else {
43-
this.client.set(key, value, function(err, res) {
49+
this.client.psetex(key, ttl, value, function(err, res) {
4450
resolve();
4551
});
4652
}

0 commit comments

Comments
 (0)