Shiran Pasternak avatar Shiran Pasternak committed db03740

Content-type now handled correctly.

Comments (0)

Files changed (3)

 TODOs
 =====
 
-* Handle non-JSON content
-* Cache multiple content types for URL routes
+* Return "Cache-Control" response header
+* Honor "Cache-Control: no-cache" request header
+* Cache multiple content types for URL routes
     clean:    false
 };
 
+var CONTENT_TYPE_REGEX = /(?:^|\s|;)(\w+(?:\/\w+))(?:$|\s|;)/;
+
 function cacheResponse(res, fn, key, cache) {
     return function (body) {
+        fn.apply(res, arguments);
+        var contentType = res.getHeader('Content-Type');
+        var ctKey = null;
+        if (contentType) {
+            ctKey = contentType.match(CONTENT_TYPE_REGEX)[1];
+        }
         // Will not cache empty values
         if (body) {
-            cache.lruSet(key, body, function (err, reply) {
-                fn.call(res, body);
-            });
-        } else {
-            fn.call(res, body);
+            cache.lruSet(key, ctKey, body, function (err, reply) {});
         }
     }
 }
     cache.lruKey = function (key) {
         return [params.prefix, key].join(":");
     };
-    cache.lruGet = function (key, fn) {
-        var lkey = cache.lruKey(key);
-        cache.get(lkey, function (err, reply) {
-            if (reply) {
-                cache.lruSet(key, reply);
-            }
-            fn(err, reply);
-        });
+    cache.lruGet = function (hash, key, fn) {
+        var lkey = cache.lruKey(hash);
+        if (key == null) {
+            cache.hkeys(lkey, function (err, keys) {
+                cache.hget(lkey, keys[0], function (err, reply) {
+                    if (reply) {
+                        cache.lruSet(hash, keys[0], reply);
+                    }
+                    fn(err, reply, [hash, keys[0]]);
+                });
+            })
+        } else {
+            cache.hget(lkey, key, function (err, reply) {
+                if (reply) {
+                    cache.lruSet(hash, key, reply);
+                }
+                fn(err, reply);
+            });
+        }
     };
-    cache.lruSet = function (key, val, fn) {
-        var lkey = cache.lruKey(key);
-        var retval = cache.set(lkey, val, function () {
+    cache.lruSet = function (hash, key, val, fn) {
+        var lkey = cache.lruKey(hash);
+        var retval = cache.hset(lkey, key, val, function () {
             if ("function" === typeof fn)
                 fn.call(null, arguments)
         });
         });
     }
     
-    var timesCalled = 0;
-    
     return function (req, res, next) {
         if (req.url.match(params.path) === null) {
             return next();
                 return next();
             }
         }
-        cache.lruGet(req.url, function (err, reply) {
-            res.set("Content-Type", "application/json");
+        cache.lruGet(req.url, null, function (err, reply, key) {
             if (reply) {
-                res.send(reply);
-                return res.end();
+                var contentType = key[1];
+                if (contentType) {
+                    res.setHeader('Content-Type', contentType);
+                }
+                return res.end(reply);
             }
             res.end = cacheResponse(res, res.end, req.url, cache);
             next();
 {
             "name": "web-cache",
      "description": "A seamless web cache",
-          "author": "Shiran Pasternak",
+          "author": {
+            "name": "Shiran Pasternak",
+           "email": "shiranpasternak@gmail.com"
+          },
          "version": "0.0.1",
         "keywords": [ "server", "cache", "redis" ],
     "dependencies": {
              "url": "http://bitbucket.org/gingi/web-cache/issues"
             },
         "licenses": [ { "type": "MIT" } ],
+         "scripts": {
+            "test": "make test"
+         },
  "devDependencies": {
          "express": ">=3.0",
            "mocha": ">=1.8.1",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.