Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
8 changes: 7 additions & 1 deletion lib/Cacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down