From c3dabc1c9229f0f81743d89cd4eced29eabffbed Mon Sep 17 00:00:00 2001 From: Simon Harper Date: Sat, 30 Jul 2016 14:57:19 +0100 Subject: [PATCH] Added redis readycheck, fallback to db query if unavailable --- lib/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index f9977ef..43a7f16 100644 --- a/lib/index.js +++ b/lib/index.js @@ -44,7 +44,10 @@ Cacher.prototype.prefix = function prefix(cachePrefix) { */ Cacher.prototype.query = function query(options) { this.options = options || this.options; - return this.fetchFromCache(); + + return redis.ready + ? this.fetchFromCache() + : this.fetchFromDatabase(this.options.key, true); }; /** @@ -58,7 +61,7 @@ Cacher.prototype.ttl = function ttl(seconds) { /** * Fetch from the database */ -Cacher.prototype.fetchFromDatabase = function fetchFromDatabase(key) { +Cacher.prototype.fetchFromDatabase = function fetchFromDatabase(key, nocache) { var method = this.model[this.method]; var self = this; this.cacheHit = false; @@ -78,6 +81,10 @@ Cacher.prototype.fetchFromDatabase = function fetchFromDatabase(key) { } else { res = results; } + + if (nocache) + return resolve(res); + return self.setCache(key, res, self.seconds) .then( function good() {