From 5e398bc81cce3b30908b1542691aee456160f7d1 Mon Sep 17 00:00:00 2001
From: Kevin Whitley <kevin3503@gmail.com>
Date: Wed, 14 Sep 2016 11:45:45 -0500
Subject: [PATCH] fixes #36

---
 lib/apicache.js | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/apicache.js b/lib/apicache.js
index c2c1f1a..0a79535 100644
--- a/lib/apicache.js
+++ b/lib/apicache.js
@@ -23,6 +23,8 @@ function ApiCache() {
     redisClient:        false
   };
 
+  var instance = this;
+
   var index = null;
 
   this.clear = function(target) {
@@ -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);
@@ -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+)$/);
 
@@ -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);
@@ -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) {
@@ -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);