[Groonga-commit] droonga/express-droonga at 6cb3b89 [master] Set cached response with callback

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Mar 20 16:01:57 JST 2014


YUKI Hiroshi	2014-03-20 16:01:57 +0900 (Thu, 20 Mar 2014)

  New Revision: 6cb3b893671b27e9310b2f95d0b6917e6b3ce96b
  https://github.com/droonga/express-droonga/commit/6cb3b893671b27e9310b2f95d0b6917e6b3ce96b

  Message:
    Set cached response with callback

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

  Modified: lib/response-cache/cache.js (+0 -3)
===================================================================
--- lib/response-cache/cache.js    2014-03-20 15:55:31 +0900 (845d31f)
+++ lib/response-cache/cache.js    2014-03-20 16:01:57 +0900 (cc204ba)
@@ -34,9 +34,6 @@ function Cache(options) {
   });
 
   this.rules = options.rules;
-  this.rules.forEach(function(rule) {
-    rule.cache = cache;
-  });
 }
 Cache.prototype = {
   'get': function(key, callback) {

  Modified: lib/response-cache/entry.js (+5 -14)
===================================================================
--- lib/response-cache/entry.js    2014-03-20 15:55:31 +0900 (6b83fbf)
+++ lib/response-cache/entry.js    2014-03-20 16:01:57 +0900 (f41e775)
@@ -1,16 +1,12 @@
-function Entry(key, ttlInMilliSeconds, cache) {
+function Entry(key, ttlInMilliSeconds) {
   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: {},
@@ -34,7 +30,7 @@ Entry.prototype = {
     this.data.headers = response.headers;
   },
 
-  hook: function(response) {
+  hook: function(response, onEndCallback) {
     var entry = this;
 
     var originalWrite = response.write;
@@ -49,16 +45,11 @@ Entry.prototype = {
       var returnValue = originalEnd.call(response, chunk, encoding);
 
       entry.finalize(response);
-      entry.store();
+      if (entry.isCachable())
+        onEndCallback(entry.data, entry.ttlInMilliSeconds);
+
       return returnValue;
     };
-  },
-
-  store: function() {
-    if (!this.isCachable())
-      return;
-
-    this.cache.set(this.key, this.data, this.ttlInMilliSeconds);
   }
 };
 exports = module.exports = Entry;

  Modified: lib/response-cache/index.js (+3 -1)
===================================================================
--- lib/response-cache/index.js    2014-03-20 15:55:31 +0900 (399d592)
+++ lib/response-cache/index.js    2014-03-20 16:01:57 +0900 (380eab1)
@@ -36,7 +36,9 @@ exports = module.exports = function(options) {
         sendCachedResponse(response, cachedResponse);
       } else {
         var entry = rule.creteEntry(cacheKey);
-        entry.hook(response);
+        entry.hook(response, function(cachedResponse, ttl) {
+          cache.set(cacheKey, cachedResponse, ttl);
+        });
         next();
       }
     });

  Modified: lib/response-cache/rule.js (+1 -1)
===================================================================
--- lib/response-cache/rule.js    2014-03-20 15:55:31 +0900 (20b4fae)
+++ lib/response-cache/rule.js    2014-03-20 16:01:57 +0900 (2ed56fd)
@@ -10,7 +10,7 @@ Rule.prototype = {
     return this.regex.test(request.url);
   },
   createEntry: function(key) {
-    return new Entry(key, this.ttlInMilliSeconds, this.cache);
+    return new Entry(key, this.ttlInMilliSeconds);
   }
 };
 exports = module.exports = Rule;
-------------- next part --------------
HTML����������������������������...
Download 



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