Comparing version
@@ -20,3 +20,3 @@ const { gzip } = require('zlib'); | ||
module.exports = function(options) { | ||
options = options || { compression: false }; | ||
options = options || { compression: false, setCachedHeader: false }; | ||
@@ -57,2 +57,3 @@ const hash = | ||
if (obj.etag) this.response.etag = obj.etag; | ||
if (options.setCachedHeader) this.response.set('X-Cached-Response', 'HIT'); | ||
if (this.request.fresh) { | ||
@@ -59,0 +60,0 @@ this.response.status = 304; |
{ | ||
"name": "koa-cash", | ||
"description": "HTTP response caching for Koa. HTTP response caching for Koa. Supports Redis, in-memory store, and more!", | ||
"version": "4.0.4", | ||
"version": "4.0.5", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -6,0 +6,0 @@ "ava": { |
@@ -93,2 +93,6 @@ # koa-cash | ||
#### `setCachedHeader` | ||
If a truthy value is passed, then `X-Cached-Response` header will be set as `HIT` when response is served from the cache. This value is `false` by default. | ||
#### `hash()` | ||
@@ -95,0 +99,0 @@ |
@@ -8,16 +8,15 @@ const LRU = require('lru-cache'); | ||
const createApp = function(c, opts) { | ||
const createApp = function(c, opts = {}) { | ||
const app = new Koa(); | ||
app.use( | ||
cash( | ||
opts || { | ||
get(key) { | ||
return c.get(key); | ||
}, | ||
set(key, value) { | ||
return c.set(key, value); | ||
}, | ||
compression: true | ||
} | ||
) | ||
cash({ | ||
get(key) { | ||
return c.get(key); | ||
}, | ||
set(key, value) { | ||
return c.set(key, value); | ||
}, | ||
compression: true, | ||
...opts | ||
}) | ||
); | ||
@@ -49,3 +48,3 @@ return app; | ||
if (await ctx.cashed()) return; | ||
throw new Error('wtf'); | ||
throw new Error('oops'); | ||
}); | ||
@@ -63,2 +62,22 @@ | ||
test.cb( | ||
'when setCachedHeader is true, serve from cache should set appropriate header', | ||
t => { | ||
const app = createApp(c, { setCachedHeader: true }); | ||
app.use(async function(ctx) { | ||
if (await ctx.cashed()) return; | ||
throw new Error('oops'); | ||
}); | ||
request(app.listen()) | ||
.get('/') | ||
.expect(200) | ||
.expect('Content-Type', 'text/lol; charset=utf-8') | ||
.expect('Content-Encoding', 'identity') | ||
.expect('X-Cached-Response', 'HIT') | ||
.expect('ETag', '"lol"') | ||
.expect('lol', t.end); | ||
} | ||
); | ||
test.cb( | ||
'when cached when the method is POST it should not serve from cache', | ||
@@ -68,3 +87,3 @@ t => { | ||
app.use(async function(ctx) { | ||
if (await ctx.cashed()) throw new Error('wtf'); | ||
if (await ctx.cashed()) throw new Error('oops'); | ||
ctx.body = 'lol'; | ||
@@ -83,3 +102,3 @@ }); | ||
if (await ctx.cashed()) return; | ||
throw new Error('wtf'); | ||
throw new Error('oops'); | ||
}); | ||
@@ -86,0 +105,0 @@ |
24846
3.4%552
3.37%194
2.11%