[Groonga-commit] droonga/express-droonga at 4b0c0c2 [master] Bind cache entry class to a rule

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Mar 20 15:09:08 JST 2014


YUKI Hiroshi	2014-03-20 15:09:08 +0900 (Thu, 20 Mar 2014)

  New Revision: 4b0c0c28949a5c6b73757552cb72af0e7ab0654a
  https://github.com/droonga/express-droonga/commit/4b0c0c28949a5c6b73757552cb72af0e7ab0654a

  Message:
    Bind cache entry class to a rule

  Modified files:
    lib/response-cache/cache.js
    lib/response-cache/index.js

  Modified: lib/response-cache/cache.js (+58 -22)
===================================================================
--- lib/response-cache/cache.js    2014-03-20 14:46:22 +0900 (b6eae5c)
+++ lib/response-cache/cache.js    2014-03-20 15:09:08 +0900 (61f7c73)
@@ -17,8 +17,10 @@ function normalizeCacheOptions(options) {
   options.rules = options.rules || [];
   options.size = options.size || defaultSie;
   normalizeTTLOption(options);
-  options.rules.forEach(function(rule) {
+
+  options.rules = options.rules.map(function(rule) {
     normalizeTTLOption(rule);
+    return new Rule(rule, options);
   });
 
   return options;
@@ -30,11 +32,44 @@ function Cache(options) {
     size: options.size
   });
 
-  this.isCachableRequest = function(request) {
-    return request.method == 'GET';
+  this.rules = options.rules;
+  this.rules.forEach(function(rule) {
+    rule.cache = cache;
+  });
+}
+Cache.prototype = {
+  'get': function(key, callback) {
+    return this.cache.get(key, callback);
+  },
+
+  'set': function(key, value, ttl, callback) {
+    return this.cache.set(key, value, ttl, callback);
+  },
+
+  getRule: function(request) {
+    if (request.method != 'GET')
+      return null;
+
+    var foundRule = null;
+    this.rules.some(function(rule) {
+      if (rule.match(request))
+        return foundRule = rule;
+    });
+    return foundRule;
   };
+};
+exports = module.exports = Cache;
+
+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.Entry = function CacheEntry(key) {
     this.key = key;
     this.data = {
       status:  0,
@@ -42,50 +77,51 @@ function Cache(options) {
       body:    []
     };
   };
-  this.Entry.prototype = {
+  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 self = this;
+      var entry = this;
 
       var originalWrite = response.write;
       response.write = function(chunk, encoding) {
-        self.write(chunk, encoding);
+        entry.write(chunk, encoding);
         return originalWrite.call(response, chunk, encoding);
       };
 
       var originalEnd = response.end;
       response.end = function(chunk, encoding) {
-        self.write(chunk, encoding);
+        entry.write(chunk, encoding);
         var returnValue = originalEnd.call(response, chunk, encoding);
 
-        self.finalize(response);
-        if (self.cachable) {
-          cache.set(self.key, self.data, options.ttlInMilliSeconds);
-        }
-
+        entry.finalize(response);
+        entry.store();
         return returnValue;
       };
+    },
+
+    store: function() {
+      if (!this.cachable)
+        return;
+
+      if (!this.cache)
+        throw new Error('no cache storage');
+
+      this.cache.set(this.key, this.data, this.ttlInMilliSeconds);
     }
   };
 }
-Cache.prototype = {
-  'get': function(key, callback) {
-    return this.cache.get(key, callback);
-  },
-  'set': function(key, value, ttl, callback) {
-    return this.cache.set(key, value, ttl, callback);
-  }
-};
-exports = module.exports = Cache;

  Modified: lib/response-cache/index.js (+3 -2)
===================================================================
--- lib/response-cache/index.js    2014-03-20 14:46:22 +0900 (5dd493e)
+++ lib/response-cache/index.js    2014-03-20 15:09:08 +0900 (a414dc3)
@@ -19,7 +19,8 @@ exports = module.exports = function(options) {
   var cache = new Cache(options);
 
   return function(request, response, next) {
-    if (!cache.isCachableRequest(request)) {
+    var rule = cache.getRule(request);
+    if (!rule) {
       next();
       return;
     }
@@ -34,7 +35,7 @@ exports = module.exports = function(options) {
       if (cachedResponse) {
         sendCachedResponse(response, cachedResponse);
       } else {
-        var entry = new cache.Entry(cacheKey);
+        var entry = new rule.CacheEntry(cacheKey);
         entry.hook(response);
         next();
       }
-------------- next part --------------
HTML����������������������������...
Download 



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