diff --git a/README.md b/README.md index b04accf..65e9ac5 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,12 @@ 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 + // listen for events to track cache rate and errors cacher.on("hit", function(key) { console.log("woohoo!") diff --git a/lib/Cacher.js b/lib/Cacher.js index ce979ae..e5575c5 100644 --- a/lib/Cacher.js +++ b/lib/Cacher.js @@ -76,6 +76,8 @@ 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 @@ -120,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 @@ -137,7 +142,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)