From f1bc30dda18ecc6ab09e158b3998fa95aba3cf12 Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Wed, 2 Oct 2013 20:30:05 +0200 Subject: [PATCH 1/4] add caching options for xhr requests. if no key was generated, treat it like nocache --- README.md | 4 ++++ lib/Cacher.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index b04accf..4ac9def 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ app.get("/long-cache", cacher.cacheDays(2), ...) // invalidation support cacher.invalidate('/home') +// if you don't want to cache xhr requests set it to false. +cacher.xhr = false + // listen for events to track cache rate and errors cacher.on("hit", function(key) { console.log("woohoo!") @@ -53,6 +56,7 @@ app.configure('development', function() { }) // override cache key generation for finer grain control +// if you don't return a `true` value, no caching will be applied cacher.genCacheKey = function(req) { return req.path + req.header('user-agent') } diff --git a/lib/Cacher.js b/lib/Cacher.js index ce979ae..407ef5b 100644 --- a/lib/Cacher.js +++ b/lib/Cacher.js @@ -76,6 +76,7 @@ Cacher.prototype.genCacheKey = function(req) { Cacher.prototype.noCaching = false Cacher.prototype.cacheHeader = 'X-Cacher-Hit' +Cacher.prototype.xhr = true Cacher.prototype.calcTtl = function(unit, value) { if (unit === 0 || value === 0 || unit === false) return 0 @@ -113,6 +114,11 @@ Cacher.prototype.cache = function(unit, value) { return next() } + // check for xhr requests. don't cache if not opted + if (req.xhr && !self.xhr) { + return next() + } + // obey cache-control control headers if (checkNoCache(req.header("cache-control")) || checkNoCache(req.header("pragma"))) { res.header(self.cacheHeader, false) @@ -124,6 +130,9 @@ Cacher.prototype.cache = function(unit, value) { var staleKey = key + ".stale" var realTtl = ttl + GEN_TIME * 2 + // if a key was not set, treat like nocache + if (!key) return next() + self.client.get(key, function(err, cacheObject) { if (err) { self.emit("error", err) From 369528e24390e658cca6848206796e7c26b291b3 Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Thu, 3 Oct 2013 15:25:51 +0200 Subject: [PATCH 2/4] make browser cache optional --- lib/Cacher.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/Cacher.js b/lib/Cacher.js index 407ef5b..d24f96f 100644 --- a/lib/Cacher.js +++ b/lib/Cacher.js @@ -76,7 +76,7 @@ Cacher.prototype.genCacheKey = function(req) { Cacher.prototype.noCaching = false Cacher.prototype.cacheHeader = 'X-Cacher-Hit' -Cacher.prototype.xhr = true +Cacher.prototype.browserCache = true Cacher.prototype.calcTtl = function(unit, value) { if (unit === 0 || value === 0 || unit === false) return 0 @@ -114,11 +114,6 @@ Cacher.prototype.cache = function(unit, value) { return next() } - // check for xhr requests. don't cache if not opted - if (req.xhr && !self.xhr) { - return next() - } - // obey cache-control control headers if (checkNoCache(req.header("cache-control")) || checkNoCache(req.header("pragma"))) { res.header(self.cacheHeader, false) @@ -130,9 +125,6 @@ Cacher.prototype.cache = function(unit, value) { var staleKey = key + ".stale" var realTtl = ttl + GEN_TIME * 2 - // if a key was not set, treat like nocache - if (!key) return next() - self.client.get(key, function(err, cacheObject) { if (err) { self.emit("error", err) @@ -146,7 +138,8 @@ Cacher.prototype.cache = function(unit, value) { return next() } - setHeaders(res, ttl) + // only if browser cache is opted + if (self.browserCache) setHeaders(res, ttl) if (!stale) { self.client.set(staleKey, STALE_REFRESH, GEN_TIME) From 9429f72c631aa7ecd877a857708f08532def9417 Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Thu, 3 Oct 2013 15:29:26 +0200 Subject: [PATCH 3/4] update readme --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4ac9def..2de9843 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ app.get("/long-cache", cacher.cacheDays(2), ...) // invalidation support cacher.invalidate('/home') -// if you don't want to cache xhr requests set it to false. -cacher.xhr = false +// if you don't want browser caching +cacher.browserCache = false // listen for events to track cache rate and errors cacher.on("hit", function(key) { @@ -56,7 +56,6 @@ app.configure('development', function() { }) // override cache key generation for finer grain control -// if you don't return a `true` value, no caching will be applied cacher.genCacheKey = function(req) { return req.path + req.header('user-agent') } From 5ea9c1ebaa53e246d46cc7e12aca8f3e9178cc92 Mon Sep 17 00:00:00 2001 From: Madhusudhan Srinivasa Date: Thu, 3 Oct 2013 17:56:28 +0200 Subject: [PATCH 4/4] add option to not cache xhr requests --- README.md | 3 +++ lib/Cacher.js | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 2de9843..65e9ac5 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ app.get("/long-cache", cacher.cacheDays(2), ...) // invalidation support cacher.invalidate('/home') +// don't cache xhr requests +cacher.xhr = false + // if you don't want browser caching cacher.browserCache = false diff --git a/lib/Cacher.js b/lib/Cacher.js index d24f96f..e5575c5 100644 --- a/lib/Cacher.js +++ b/lib/Cacher.js @@ -77,6 +77,7 @@ Cacher.prototype.genCacheKey = function(req) { Cacher.prototype.noCaching = false Cacher.prototype.cacheHeader = 'X-Cacher-Hit' Cacher.prototype.browserCache = true +Cacher.prototype.xhr = true Cacher.prototype.calcTtl = function(unit, value) { if (unit === 0 || value === 0 || unit === false) return 0 @@ -121,6 +122,9 @@ Cacher.prototype.cache = function(unit, value) { return next() } + // handle xhr caching + if (req.xhr && !self.xhr) return next() + var key = self.genCacheKey(req) var staleKey = key + ".stale" var realTtl = ttl + GEN_TIME * 2