From 801308d9b726aa43f92d2764522f487e080df0bc Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Wed, 2 Nov 2016 15:13:55 -0700 Subject: [PATCH] RedisCacheAdapter: add some additional checks and defaults (#2991) --- src/Adapters/Cache/RedisCacheAdapter.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Adapters/Cache/RedisCacheAdapter.js b/src/Adapters/Cache/RedisCacheAdapter.js index e3611c1f..824c59c5 100644 --- a/src/Adapters/Cache/RedisCacheAdapter.js +++ b/src/Adapters/Cache/RedisCacheAdapter.js @@ -1,7 +1,7 @@ import redis from 'redis'; import logger from '../../logger'; -const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds +const DEFAULT_REDIS_TTL = 30 * 1000; // 30 seconds in milliseconds function debug() { logger.debug.apply(logger, ['RedisCacheAdapter', ...arguments]); @@ -30,17 +30,23 @@ export class RedisCacheAdapter { return this.p; } - put(key, value, ttl) { + put(key, value, ttl = DEFAULT_REDIS_TTL) { value = JSON.stringify(value); debug('put', key, value, ttl); + if (ttl === 0) { + return this.p; // ttl of zero is a logical no-op, but redis cannot set expire time of zero + } + if (ttl < 0 || isNaN(ttl)) { + ttl = DEFAULT_REDIS_TTL; + } this.p = this.p.then(() => { return new Promise((resolve, _) => { - if (ttl) { - this.client.psetex(key, ttl, value, function(err, res) { + if (ttl === Infinity) { + this.client.set(key, value, function(err, res) { resolve(); }); } else { - this.client.set(key, value, function(err, res) { + this.client.psetex(key, ttl, value, function(err, res) { resolve(); }); }