From 9e7ebcc7b8f7b8f7d488abf3c0d6a2853f95cee4 Mon Sep 17 00:00:00 2001 From: Dmitrii Prosianik Date: Tue, 5 Nov 2019 11:43:49 +0300 Subject: [PATCH] added per request cache time --- src/apicache.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/apicache.js b/src/apicache.js index ac90887..f78d234 100644 --- a/src/apicache.js +++ b/src/apicache.js @@ -387,7 +387,16 @@ function ApiCache() { return defaultDuration } - this.getDuration = function(duration) { + /** + * Function that parses cache duration string to milliseconds + * Can be overriden with custom logic depending on request and response + * Sic! Use "appendKey" option to generate custom cache key for this case! + * @param {string|number} duration + * @param {Object} req Express request + * @param {Object} res Express response + * @returns {number} cache time in ms + */ + this.getDuration = function(duration, req, res) { return parseDuration(duration, globalOptions.defaultDuration) } @@ -414,7 +423,6 @@ function ApiCache() { } this.middleware = function cache(strDuration, middlewareToggle, localOptions) { - var duration = instance.getDuration(strDuration) var opt = {} middlewareOptions.push({ @@ -591,6 +599,8 @@ function ApiCache() { performanceArray.push(perf) var cache = function(req, res, next) { + var duration = instance.getDuration(strDuration, req, res) + function bypass() { debug('bypass detected, skipping cache.') return next()