bookshelf-cache-redis
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "bookshelf-cache-redis", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A robust, performance-focused caching solution for Bookshelf based on top of Redis.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -29,3 +29,2 @@ /** | ||
module.exports = (bookshelf, settings) => { | ||
/** | ||
@@ -68,5 +67,12 @@ * @method Model#retrieveCache | ||
// Generic function to override default fetch methods. | ||
async function retrieveCache (options, method) { | ||
const serial = options.hasOwnProperty('serial') ? options.serial : undefined; | ||
async function retrieveCache(options, method) { | ||
const serial = options.hasOwnProperty('serial') | ||
? options.serial | ||
: undefined; | ||
const expired = options.hasOwnProperty('expired') | ||
? options.expired | ||
: 60 * 60; // Default expiration time to 1 hour. | ||
delete options.serial; | ||
delete options.expired; | ||
@@ -77,20 +83,18 @@ if (!serial || disabled === true) { | ||
return redis.get(serial) | ||
.then(async result => { | ||
if (result === null) { | ||
const cache = await this[method](options) | ||
.then(data => data.toJSON()); | ||
return redis.get(serial).then(async result => { | ||
if (result === null) { | ||
const cache = await this[method](options).then(data => data.toJSON()); | ||
// Store record | ||
redis.set(serial, JSON.stringify(cache)); | ||
// Store record | ||
redis.set(serial, JSON.stringify(cache), 'ex', expired); | ||
return ({ | ||
toJSON: () => cache | ||
}); | ||
} | ||
return { | ||
toJSON: () => cache | ||
}; | ||
} | ||
return ({ | ||
toJSON: () => JSON.parse(result) | ||
}); | ||
}); | ||
return { | ||
toJSON: () => JSON.parse(result) | ||
}; | ||
}); | ||
} | ||
@@ -110,41 +114,37 @@ | ||
bookshelf.Model.prototype.fetchAllCache = function (options) { | ||
bookshelf.Model.prototype.fetchAllCache = function(options) { | ||
return retrieveCache.apply(this, [options, 'fetchAll']); | ||
} | ||
}; | ||
bookshelf.Model.fetchAllCache = function (...args) { | ||
bookshelf.Model.fetchAllCache = function(...args) { | ||
return this.forge().retrieveCache(...args, 'fetchAll'); | ||
} | ||
}; | ||
bookshelf.Collection.prototype.fetchAllCache = function (...args) { | ||
bookshelf.Collection.prototype.fetchAllCache = function(...args) { | ||
return fetchAllCache.apply(this.model.forge(), ...args); | ||
}; | ||
bookshelf.Model.prototype.fetchPageCache = function (options) { | ||
bookshelf.Model.prototype.fetchPageCache = function(options) { | ||
return retrieveCache.apply(this, [options, 'fetchPage']); | ||
} | ||
}; | ||
bookshelf.Model.fetchPageCache = function (...args) { | ||
bookshelf.Model.fetchPageCache = function(...args) { | ||
return this.forge().retrieveCache(...args, 'fetchPage'); | ||
} | ||
}; | ||
bookshelf.Collection.prototype.fetchPageCache = function (...args) { | ||
bookshelf.Collection.prototype.fetchPageCache = function(...args) { | ||
return retrieveCache.apply(this.model.forge(), ...args); | ||
}; | ||
bookshelf.Model.prototype.fetchCache = function (options) { | ||
bookshelf.Model.prototype.fetchCache = function(options) { | ||
return retrieveCache.apply(this, [options, 'fetch']); | ||
} | ||
}; | ||
bookshelf.Model.fetchCache = function (...args) { | ||
bookshelf.Model.fetchCache = function(...args) { | ||
return this.forge().retrieveCache(...args, 'fetch'); | ||
} | ||
}; | ||
bookshelf.Collection.prototype.fetchCache = function (...args) { | ||
bookshelf.Collection.prototype.fetchCache = function(...args) { | ||
return fetchCache.apply(this.model.forge(), ...args); | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7512
127