Commits

Shiran Pasternak  committed cb5551e

Using Redis for LRU cache.

  • Participants
  • Parent commits 7787a6d

Comments (0)

Files changed (3)

+/node_modules

File package.json

+{
+            "name": "web-cache",
+     "description": "A seamless web cache",
+          "author": "Shiran Pasternak",
+         "version": "0.0.1",
+        "keywords": [ "server", "cache", "redis" ],
+    "dependencies": {
+           "redis": ">=0.8.2"
+    }
+}

File web-cache.js

-var redis    = require('redis').createClient();
+var Redis = require('redis');
 
-function cacheResponse(res, fn, key) {
+var ONE_DAY = 86400;
+
+var DEFAULTS = {
+    path:     '/',
+    maxItems: 5000,
+    expire:   ONE_DAY,
+    prefix:   "web-cache"
+};
+
+function cacheResponse(res, fn, key, cache) {
     return function (body) {
+        // Will not cache empty values
         if (body) {
-            var value = JSON.stringify(body);
-            redis.set(key, value);
+            cache.lruSet(key, body);
         }
         fn.call(res, body);
     }
 }
 
+function initializeParams(inParams) {
+    var params = {};
+    for (var property in DEFAULTS) { params[property] = DEFAULTS[property]; }
+    for (var property in inParams) { params[property] = inParams[property]; }
+    return params;
+}
+
+function lruDecorate(cache, params) {
+    cache.lruGet = function (key, fn) {
+        cache.hget(params.prefix, key, function (err, reply) {
+            if (reply) {
+                reply = JSON.parse(reply);
+                cache.lruSet(key, reply);
+            }
+            fn(err, reply);
+        });
+    };
+    cache.lruSet = function (key, val, fn) {
+        var retval = cache.hset(params.prefix, key, JSON.stringify(data), fn);
+        cache.expire(params.prefix, key, params.expire);
+        return retval;
+    }
+}
+
 exports.middleware = function (params) {
-    params      = params      || {};
-    params.path = params.path || '/';
+    params = initializeParams(params);
+    
+    var cache = lruDecorate(Redis.createClient(params.port, params.host));
+    
+    if ("string" !== typeof params.prefix) {
+        throw new Error("'prefix' property must be a string");
+    }
     if (!params.path instanceof RegExp) {
         params.path = new RegExp("^" + params.path);
     }
             }
         }
         res.contentType = 'application/json';
-        redis.get(req.url, function (err, reply) {
-            if (reply && reply !== "") {
-                var body = JSON.parse(reply);
-                if (body !== null && body !== {}) {
-                    return res.send(JSON.parse(reply));
-                }
+        
+        cache.lruGet(req.url, function (err, reply) {
+            if (reply) {
+                return res.send(body);
             }
-            var original = res.end;
-            res.end = cacheResponse(res, original, req.url);
+            res.end = cacheResponse(res, res.end, req.url, cache);
             next();
         });
     }
-}
+}