|
1 | 1 | import redis from 'redis';
|
2 | 2 | import logger from '../../logger';
|
3 | 3 |
|
4 |
| -const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds |
| 4 | +const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds in milliseconds |
5 | 5 |
|
6 | 6 | function debug() {
|
7 | 7 | logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]);
|
@@ -30,17 +30,23 @@ export class RedisCacheAdapter {
|
30 | 30 | return this.p;
|
31 | 31 | }
|
32 | 32 |
|
33 |
| - put(key, value, ttl) { |
| 33 | + put(key, value, ttl = DEFAULT_REDIS_TTL) { |
34 | 34 | value = JSON.stringify(value);
|
35 | 35 | 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 | + } |
36 | 42 | this.p = this.p.then(() => {
|
37 | 43 | 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) { |
40 | 46 | resolve();
|
41 | 47 | });
|
42 | 48 | } else {
|
43 |
| - this.client.set(key, value, function(err, res) { |
| 49 | + this.client.psetex(key, ttl, value, function(err, res) { |
44 | 50 | resolve();
|
45 | 51 | });
|
46 | 52 | }
|
|
0 commit comments