From 1382ec47b2b790a253d9d10efd25f61cd8baead2 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Mon, 21 Nov 2011 22:53:50 -0500 Subject: [PATCH] DRY up the static cache --- TODO | 1 - lib/static_handler.js | 60 +++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/TODO b/TODO index 41ff748..122ad6e 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,6 @@ add feedback for errors to UI - esp. too long add about page make asset caching optional todo store buffer instead of string while grabbing contents (if possible) -write cache to use callback instead of using non-DRY code # shared version only some way to do announcements easily (and use for ads) diff --git a/lib/static_handler.js b/lib/static_handler.js index 07e2f47..9701a7c 100644 --- a/lib/static_handler.js +++ b/lib/static_handler.js @@ -35,40 +35,50 @@ StaticHandler.prototype.handle = function(incPath, response) { // Go to index if not found or / if (!this.availablePaths[incPath]) incPath = this.defaultPath; var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); - // And then stream the file back - if (StaticHandler.cache[filePath]) { - this.serveCached(filePath, response); - } - else { - this.retrieve(filePath, response); - } -}; - -// Retrieve from the file -StaticHandler.prototype.retrieve = function(filePath, response) { - var _this = this; - fs.readFile(filePath, function(error, content) { - if (error) { - winston.error('unable to read file', { path: filePath, error: error.message }); - response.writeHead(500, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'IO: Unable to read file' })); - } - else { + // And then stream the file back - either from the cache or from source + var cached = this.isCached(filePath); + var method = cached ? this.serveCached : this.retrieve; + // Run! + method(filePath, function(error, content) { + // Get the content + if (content) { var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); response.writeHead(200, { 'content-type': contentType }); response.end(content, 'utf-8'); - // Stick it in the cache - StaticHandler.cache[filePath] = content; + // Stick it in the cache if its not in there + if (!cached) { + StaticHandler.cache[filePath] = content; + } } + // Or break down and cry + else { + winston.error('unable to read file', { path: filePath, error: error }); + response.writeHead(500, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ message: 'IO: Unable to read file' })); + // If it was cached, bust the cache + if (cached) { + StaticHandler.cache[filePath] = null; + } + } + }); +}; + +// Retrieve from the file +StaticHandler.prototype.retrieve = function(filePath, callback) { + var _this = this; + fs.readFile(filePath, function(error, content) { + callback(error, content); }); }; // Retrieve from memory cache -StaticHandler.prototype.serveCached = function(filePath, response) { - var contentType = StaticHandler.contentTypeFor(path.extname(filePath)); - response.writeHead(200, { 'content-type': contentType }); - response.end(StaticHandler.cache[filePath], 'utf-8'); +StaticHandler.prototype.serveCached = function(filePath, callback) { + callback(undefined, StaticHandler.cache[filePath]); }; +// Determine if a given filePath is cached or not +StaticHandler.prototype.isCached = function(filePath) { + return !!StaticHandler.cache[filePath]; +}; module.exports = StaticHandler;