Skip to content

Commit

Permalink
fixes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Whitley committed Sep 14, 2016
1 parent 9cda4e0 commit 5e398bc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/apicache.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function ApiCache() {
redisClient: false
};

var instance = this;

var index = null;

this.clear = function(target) {
Expand All @@ -48,7 +50,7 @@ function ApiCache() {
delete index.groups[target];
} else if (target) {
if (globalOptions.debug) {
console.log('[api-cache]: clearing key: ', target);
console.log('[api-cache]: clearing cached entry for "' + target + '"');
}
if (!globalOptions.redisClient) {
memCache.del(target);
Expand Down Expand Up @@ -86,6 +88,8 @@ function ApiCache() {
};

this.middleware = function cache(duration, middlewareToggle) {
var strDuration = duration;

if (typeof duration === 'string') {
var split = duration.match(/^(\d+)\s(\w+)$/);

Expand Down Expand Up @@ -204,7 +208,7 @@ function ApiCache() {
if (!memCache.get(key) && !req.headers['x-apicache-bypass']) {
if (req.apicacheGroup) {
if (globalOptions.debug) {
console.log('[api-cache]: group detected: ' + req.apicacheGroup);
console.log('[api-cache]: group detected "' + req.apicacheGroup + '"');
}
index.groups[req.apicacheGroup] = index.groups[req.apicacheGroup] || [];
index.groups[req.apicacheGroup].push(key);
Expand All @@ -213,7 +217,7 @@ function ApiCache() {
index.all.push(key);

if (globalOptions.debug) {
console.log('[api-cache]: adding cache entry for "' + key + '" @ ' + duration + ' milliseconds');
console.log('[api-cache]: adding cache entry for "' + key + '" @ ' + strDuration);
}

_.each(['Cache-Control', 'Expires', 'Content-Encoding'], function(h) {
Expand All @@ -229,6 +233,8 @@ function ApiCache() {
globalOptions.redisClient.hset(key, "responseObj", JSON.stringify(responseObj));
globalOptions.redisClient.hset(key, "duration", duration);
}

setTimeout(function() { instance.clear(key) }, duration);
}

return res.realSend(responseObj.body);
Expand Down

0 comments on commit 5e398bc

Please sign in to comment.