diff --git a/lib/prerenderRedisCache.js b/lib/prerenderRedisCache.js index 8fadd15..971b934 100644 --- a/lib/prerenderRedisCache.js +++ b/lib/prerenderRedisCache.js @@ -7,10 +7,14 @@ var redis_url = process.env.REDISTOGO_URL || process.env.REDISCLOUD_URL || proce url = require('url'), ttl = process.env.PAGE_TTL || 86400; + var zlib = require('zlib'), + gzip_level = parseInt(process.env.GZIP_LEVEL) || 0; + + // Parse out the connection vars from the env string. var connection = url.parse(redis_url), redis = require('redis'), - client = redis.createClient(connection.port, connection.hostname), + client = redis.createClient(connection.port, connection.hostname, {'return_buffers': true}), redis_online = false, last_error = "", last_end_message = ""; // Make redis connection @@ -22,9 +26,9 @@ if (connection.auth) { // Catch all error handler. If redis breaks for any reason it will be reported here. client.on("error", function (err) { - if(last_error === err.toString()) { + if(last_error === err.toString()) { // Swallow the error for now - } else { + } else { last_error = err.toString(); console.log("Redis Cache Error: " + err); } @@ -38,9 +42,9 @@ client.on("ready", function () { client.on("end", function (err) { if(err) { err = err.toString(); - if(last_end_message == err) { + if(last_end_message == err) { // Swallow the error for now - } else { + } else { last_end_message = err; redis_online = false; console.log("Redis Cache Conncetion Closed. Will now bypass redis until it's back."); @@ -48,17 +52,41 @@ client.on("end", function (err) { } }); +function redisSet(key, val) { + client.set(key, val, function (err, reply) { + // If library set to cache set an expiry on the key. + if (!err && reply && ttl && ttl != 0) { + client.expire(key, ttl, function (err, didSetExpiry) { + console.warn(!!didSetExpiry); + }); + } + }); +} + module.exports = { beforePhantomRequest: function (req, res, next) { - // + // if (req.method !== 'GET' || redis_online !== true) { return next(); } client.get(req.prerender.url, function (err, result) { + // Page found - return to prerender and 200 if (!err && result) { - res.send(200, result); + if (gzip_level > 0) { + zlib.gunzip(result, function (gzip_err, gunzip_result) { + if (!gzip_err && gunzip_result) { + res.send(200, gunzip_result); + } + else { + console.log("Failed to decompress key value, maybe you should flush your cache?"); + next(); + } + }); + } else { + res.send(200, result); + } } else { next(); } @@ -71,15 +99,17 @@ module.exports = { } // Don't cache anything that didn't result in a 200. This is to stop caching of 3xx/4xx/5xx status codes if (req.prerender.statusCode === 200) { - client.set(req.prerender.url, req.prerender.documentHTML, function (err, reply) { - // If library set to cache set an expiry on the key. - if (!err && reply && ttl && ttl != 0) { - client.expire(req.prerender.url, ttl, function (err, didSetExpiry) { - console.warn(!!didSetExpiry); - }); - } - }); + if (gzip_level > 0) { + zlib.gzip(req.prerender.documentHTML, {'level': gzip_level}, function (gzip_err, gzip_result) { + if (!gzip_err) { + redisSet(req.prerender.url, gzip_result); + } + }); + } else { + redisSet(req.prerender.url, req.prerender.documentHTML); + } + } next(); } -}; \ No newline at end of file +};