[Groonga-commit] droonga/express-droonga at 375d9a5 [master] Extract cache entry class to a file

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Mar 20 15:18:50 JST 2014


YUKI Hiroshi	2014-03-20 15:18:50 +0900 (Thu, 20 Mar 2014)

  New Revision: 375d9a57881fcc1680dc97e63a2ec57c4ae54d00
  https://github.com/droonga/express-droonga/commit/375d9a57881fcc1680dc97e63a2ec57c4ae54d00

  Message:
    Extract cache entry class to a file

  Added files:
    lib/response-cache/entry.js
  Modified files:
    lib/response-cache/index.js
    lib/response-cache/rule.js

  Added: lib/response-cache/entry.js (+64 -0) 100644
===================================================================
--- /dev/null
+++ lib/response-cache/entry.js    2014-03-20 15:18:50 +0900 (736e677)
@@ -0,0 +1,64 @@
+function Entry(key, ttlInMilliSeconds, cache) {
+  if (!key)
+    throw new Error('invalid cache key');
+
+  if (!ttlInMilliSeconds)
+    throw new Error('invalid TTL');
+
+  if (!cache)
+    throw new Error('invalid storage');
+
+  this.key = key;
+  this.ttlInMilliSeconds = ttlInMilliSeconds;
+  this.cache = cache;
+  this.data = {
+    status:  0,
+    headers: {},
+    body:    []
+  };
+};
+Entry.prototype = {
+  get cachable() {
+    return this.data.status == 200;
+  },
+
+  write: function(chunk, encoding) {
+    this.data.body.push({
+      data:     chunk,
+      encoding: encoding
+    });
+  },
+
+  finalize: function(response) {
+    this.data.status = response.statusCode;
+    this.data.headers = response.headers;
+  },
+
+  hook: function(response) {
+    var entry = this;
+
+    var originalWrite = response.write;
+    response.write = function(chunk, encoding) {
+      entry.write(chunk, encoding);
+      return originalWrite.call(response, chunk, encoding);
+    };
+
+    var originalEnd = response.end;
+    response.end = function(chunk, encoding) {
+      entry.write(chunk, encoding);
+      var returnValue = originalEnd.call(response, chunk, encoding);
+
+      entry.finalize(response);
+      entry.store();
+      return returnValue;
+    };
+  },
+
+  store: function() {
+    if (!this.cachable)
+      return;
+
+    this.cache.set(this.key, this.data, this.ttlInMilliSeconds);
+  }
+};
+exports = module.exports = Entry;

  Modified: lib/response-cache/index.js (+1 -1)
===================================================================
--- lib/response-cache/index.js    2014-03-20 15:12:43 +0900 (a414dc3)
+++ lib/response-cache/index.js    2014-03-20 15:18:50 +0900 (399d592)
@@ -35,7 +35,7 @@ exports = module.exports = function(options) {
       if (cachedResponse) {
         sendCachedResponse(response, cachedResponse);
       } else {
-        var entry = new rule.CacheEntry(cacheKey);
+        var entry = rule.creteEntry(cacheKey);
         entry.hook(response);
         next();
       }

  Modified: lib/response-cache/rule.js (+10 -61)
===================================================================
--- lib/response-cache/rule.js    2014-03-20 15:12:43 +0900 (eafd04c)
+++ lib/response-cache/rule.js    2014-03-20 15:18:50 +0900 (9c3836a)
@@ -1,66 +1,15 @@
+var Entry = require('./entry');
+
 function Rule(rule, options) {
   this.regex = rule.regex;
   this.ttlInMilliSeconds = rule.ttlInMilliSeconds || options.ttlInMilliSeconds;
-
-  var self = this;
-
-  this.CacheEntry = function CacheEntry(key) {
-    if (!key)
-      throw new Error('invalid cache key');
-
-    this.key = key;
-    this.data = {
-      status:  0,
-      headers: {},
-      body:    []
-    };
-  };
-  this.CacheEntry.prototype = {
-    get cachable() {
-      return this.data.status == 200;
-    },
-
-    write: function(chunk, encoding) {
-      this.data.body.push({
-        data:     chunk,
-        encoding: encoding
-      });
-    },
-
-    finalize: function(response) {
-      this.data.status = response.statusCode;
-      this.data.headers = response.headers;
-    },
-
-    hook: function(response) {
-      var entry = this;
-
-      var originalWrite = response.write;
-      response.write = function(chunk, encoding) {
-        entry.write(chunk, encoding);
-        return originalWrite.call(response, chunk, encoding);
-      };
-
-      var originalEnd = response.end;
-      response.end = function(chunk, encoding) {
-        entry.write(chunk, encoding);
-        var returnValue = originalEnd.call(response, chunk, encoding);
-
-        entry.finalize(response);
-        entry.store();
-        return returnValue;
-      };
-    },
-
-    store: function() {
-      if (!this.cachable)
-        return;
-
-      if (!self.cache)
-        throw new Error('no cache storage');
-
-      self.cache.set(this.key, this.data, this.ttlInMilliSeconds);
-    }
-  };
 }
+Rule.prototype = {
+  match: function(request) {
+    return this.regex.test(request.url);
+  },
+  createEntry: function(key) {
+    return new Entry(key, this.ttlInMilliSeconds, this.cache);
+  }
+};
 exports = module.exports = Rule;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index