core.io-cache-redis
Advanced tools
Comparing version 0.4.0 to 0.5.0
'use strict'; | ||
const promiseTimeout = require('p-timeout'); | ||
const crypto = require('crypto'); | ||
const extend = require('gextend'); | ||
const EventEmitter = require('events'); | ||
const { CacheClientError } = require('./errors'); | ||
@@ -28,2 +29,20 @@ const defaults = require('./defaults'); | ||
let connectionId; | ||
/** | ||
* TODO: This will most likely generate | ||
* an uncaught error since is an async | ||
* error from the constructor. For now | ||
* it is ok since we want to throw if | ||
* the redis connection is not ok. | ||
*/ | ||
if (config.clientConnectionTimeout) { | ||
connectionId = setTimeout(_ => { | ||
this.logger.warn('Client connection time out.'); | ||
this.logger.warn('If your service needs TLS ensure your config is ok'); | ||
this.logger.warn('client option tls=%s', this.clientOptions.tls); | ||
throw new CacheClientError('Client connection time out', code); | ||
}, config.clientConnectionTimeout); | ||
} | ||
this.client.on('error', error => { | ||
@@ -41,2 +60,3 @@ this.handleError(error, 'Redis client error'); | ||
this.logger.info('redis ready event...'); | ||
clearTimeout(connectionId); | ||
this.emit('ready', event); | ||
@@ -67,2 +87,3 @@ }); | ||
* @param {Int} [options.ttl=defaultTTL] TTL for this key | ||
* @param {Int} options.timeout Timeout in milliseconds for fallback function | ||
* @param {Boolean} [options.deserialize=true] Retrieve content as JSON | ||
@@ -95,3 +116,11 @@ * @param {Boolean} [options.addTimestamp=true] Include a timestamp to payload | ||
value = await fallback(); | ||
/** | ||
* If we pass a timeout then listen for | ||
* timeout errors. | ||
*/ | ||
if (options.timeout) { | ||
value = await promiseTimeout(fallback(), options.timeout, new CacheClientError('Fallback function timeout', 408)); | ||
} else { | ||
value = await fallback(); | ||
} | ||
@@ -110,3 +139,8 @@ /** | ||
this.handleError(error, 'cache try error'); | ||
if (options.throwOnError) throw error; | ||
/** | ||
* If we had a timeout error then throw | ||
*/ | ||
if (error.code === 408) throw error; | ||
else if (options.throwOnError) throw error; | ||
} | ||
@@ -113,0 +147,0 @@ |
@@ -6,2 +6,4 @@ /*jshint esversion:6, node:true*/ | ||
const createClient = require('./createClient'); | ||
const _5_seconds = 5 * 1000; | ||
const _24_hours = (1 * 24 * 60 * 60 * 1000); | ||
@@ -11,5 +13,13 @@ module.exports = { | ||
logger: extend.shim(console), | ||
defaultTTL: (1 * 24 * 60 * 60 * 1000), | ||
/** | ||
* TTL value for expire | ||
* keys in milliseconds by default. | ||
*/ | ||
defaultTTL: _24_hours, | ||
/** | ||
* Use TTL as seconds? | ||
*/ | ||
ttlInSeconds: false, | ||
clientConnectionTimeout: _5_seconds, | ||
lastError: null, | ||
ttlInSeconds: false, | ||
errors: [], | ||
@@ -16,0 +26,0 @@ createClient, |
@@ -19,3 +19,3 @@ 'use strict'; | ||
/** | ||
* Expose a function to retrieve | ||
* Expose a function to retrieve | ||
* the redis client so that others can | ||
@@ -22,0 +22,0 @@ * use it |
{ | ||
"name": "core.io-cache-redis", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Redis cache module", | ||
@@ -26,8 +26,8 @@ "main": "index.js", | ||
"bogota": "^2.0.4", | ||
"ioredis-mock": "^5.2.0", | ||
"nyc": "^10.3.2", | ||
"proxyquire": "^1.7.11", | ||
"sinon": "^7.3.2", | ||
"ioredis-mock": "^7.1.0", | ||
"nyc": "^15.1.0", | ||
"proxyquire": "^2.1.3", | ||
"sinon": "^13.0.1", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.11.0", | ||
"tape": "^5.5.2", | ||
"tape-catch": "^1.0.6", | ||
@@ -38,4 +38,5 @@ "watch": "^1.0.2" | ||
"gextend": "*", | ||
"ioredis": "^4.19.4" | ||
"ioredis": "^4.28.5", | ||
"p-timeout": "^4.1.0" | ||
} | ||
} |
17369
10
355
3
+ Addedp-timeout@^4.1.0
+ Addedp-timeout@4.1.0(transitive)
Updatedioredis@^4.28.5