@dealerslink/node-object-key-cache
Advanced tools
Comparing version 1.1.5 to 1.2.0
35
index.js
@@ -77,5 +77,8 @@ // index.js | ||
connect() { | ||
if (this.connected) { | ||
return Promise.reject(new Error('Object Key Cache is already connected')); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.memCache = memCache.createClient(this.cacheConfig); | ||
this.creds.port = this.creds.port || 3306; | ||
this.creds.port = this.creds.port || 6379; | ||
if (__.isUnset(this.creds.host)) { | ||
@@ -90,5 +93,10 @@ this.logger.debug('Cache Connected (Memory)'); | ||
this.logger.debug('Cache Connected (Redis)'); | ||
// This also has the benefit of automatically switching to the Redis client when it becomes | ||
// available, if there was a connection problem initially | ||
this.cache = this.redisCache; | ||
// Only resolve when not already connected | ||
if (!this.connected) { | ||
resolve(this.cache); | ||
} | ||
this.connected = true; | ||
resolve(this.cache); | ||
}); | ||
@@ -100,4 +108,7 @@ this.redisCache.once('error', (err) => { | ||
this.cache = this.memCache; | ||
// Only resolve when not already connected | ||
if (!this.connected) { | ||
resolve(this.cache); | ||
} | ||
this.connected = true; | ||
resolve(this.cache); | ||
} else { | ||
@@ -108,6 +119,18 @@ this.logger.debug('Cache Connection Failed'); | ||
}); | ||
this.redisCache.on('connect', () => { | ||
if (this.connected && this.cache && this.cache instanceof MemoryCache) { | ||
this.logger.debug('Redis connection came back, reverting from MemoryCache to RedisClient'); | ||
} | ||
}); | ||
this.redisCache.on('error', (err) => { | ||
// Trap and log any subsequent errors that may get called after we've already caught the first | ||
// Redis will bomb out without this if there is no connection | ||
this.logger.error(err); | ||
// Trap and log any non ECONNREFUSED errors that may get called after we've already caught the first | ||
// Redis will bomb out without this if there is no connection as ECONNREFUSED is called multiple times | ||
// as the Redis client attempts to reconnect continuously | ||
if (__.isUnset(err.errno) || (err.errno && err.errno !== 'ECONNREFUSED')) { | ||
this.logger.error(err); | ||
} | ||
if (err.errno && err.errno === 'ECONNREFUSED' && this.connected) { | ||
this.logger.debug('Redis connection went away, reverting to MemoryCache'); | ||
this.cache = this.memCache; | ||
} | ||
}); | ||
@@ -114,0 +137,0 @@ } |
{ | ||
"name": "@dealerslink/node-object-key-cache", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "A Promise-based, object-key, cache extension for Redis and MemoryCache.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
31255
1077