Skip to content

Added redis readycheck, fallback to db query if unavailable #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ Cacher.prototype.prefix = function prefix(cachePrefix) {
*/
Cacher.prototype.run = function run(options) {
this.options = options || this.options;
return this.fetchFromCache();

return redis.ready
? this.fetchFromCache()
: this.fetchFromDatabase(this.options.key, true);
};

/**
Expand Down Expand Up @@ -124,8 +127,8 @@ Cacher.prototype.ttl = function ttl(seconds) {
/**
* Fetch from the database
*/
Cacher.prototype.fetchFromDatabase = function fetchFromDatabase(key) {
var method = this.md[this.method];
Cacher.prototype.fetchFromDatabase = function fetchFromDatabase(key, nocache) {
var method = this.model[this.method];
var self = this;
this.cacheHit = false;
return new Promise(function promiser(resolve, reject) {
Expand All @@ -144,6 +147,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() {
Expand Down