YUKI Hiroshi
null+****@clear*****
Thu Mar 20 14:35:58 JST 2014
YUKI Hiroshi 2014-03-20 14:35:58 +0900 (Thu, 20 Mar 2014) New Revision: 6db1411fbe0199c658e490f259cd9d01f3b531aa https://github.com/droonga/express-droonga/commit/6db1411fbe0199c658e490f259cd9d01f3b531aa Message: Bind operations to hook response to the Entry class Modified files: lib/response-cache.js Modified: lib/response-cache.js (+29 -25) =================================================================== --- lib/response-cache.js 2014-03-20 14:32:31 +0900 (f8a0018) +++ lib/response-cache.js 2014-03-20 14:35:58 +0900 (cdf757b) @@ -42,9 +42,10 @@ function Cache(options) { return request.method == 'GET'; }; - this.Entry = function ResponseCacheEntry(cache) { + this.Entry = function ResponseCacheEntry(key) { + this.key = key; this.data = { - status: 0, + status: 0, headers: {}, body: [] }; @@ -62,6 +63,28 @@ function Cache(options) { finalize: function(response) { this.data.status = response.statusCode; this.data.headers = response.headers; + }, + hook: function(response) { + var self = this; + + var originalWrite = response.write; + response.write = function(chunk, encoding) { + self.write(chunk, encoding); + return originalWrite.call(response, chunk, encoding); + }; + + var originalEnd = response.end; + response.end = function(chunk, encoding) { + self.write(chunk, encoding); + var returnValue = originalEnd.call(response, chunk, encoding); + + self.finalize(response); + if (self.cachable) { + cache.set(self.key, self.data, options.ttlInMilliSeconds); + } + + return returnValue; + }; } }; } @@ -83,9 +106,6 @@ exports = module.exports = function(options) { return; } - var originalWrite = response.write; - var originalEnd = response.end; - var cacheKey = generateKey(request); cache.get(cacheKey, function(error, cachedResponse) { if (error) { @@ -95,27 +115,11 @@ exports = module.exports = function(options) { if (cachedResponse) { sendCachedResponse(response, cachedResponse); - return; + } else { + var entry = new cache.Entry(cacheKey); + entry.hook(response); + next(); } - - var entry = new cache.Entry(); - - response.write = function(chunk, encoding) { - entry.write(chunk, encoding); - return originalWrite.call(response, chunk, encoding); - }; - - response.end = function(chunk, encoding) { - entry.write(chunk, encoding); - var returnValue = originalEnd.call(response, chunk, encoding); - - entry.finalize(response); - if (entry.cachable) { - cache.set(cacheKey, entry.data, options.ttlInMilliSeconds); - } - - return returnValue; - }; }); }; }; -------------- next part -------------- HTML����������������������������...Download