cacheable-response
Advanced tools
Comparing version 2.5.3 to 2.6.0
@@ -5,2 +5,9 @@ # Changelog | ||
## 2.6.0 (2021-08-19) | ||
### Features | ||
* rename getKey into key ([5ae0a1f](https://github.com/Kikobeats/cacheable-response/commit/5ae0a1f2952001f97a0903c5537d1946e8a0a056)) | ||
### 2.5.3 (2021-08-18) | ||
@@ -7,0 +14,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://nicedoc.io/Kikobeats/cacheable-response", | ||
"version": "2.5.3", | ||
"version": "2.6.0", | ||
"main": "src/index.js", | ||
@@ -8,0 +8,0 @@ "author": { |
@@ -115,2 +115,28 @@ # cacheable-response | ||
Additionally, you can configure a stale ttl: | ||
```js | ||
const cacheableResponse = require('cacheable-response') | ||
const ssrCache = cacheableResponse({ | ||
get: ({ req, res }) => ({ | ||
data: doSomething(req), | ||
ttl: 86400000, // 24 hours | ||
staleTtl: 3600000 // 1h | ||
}), | ||
send: ({ data, res, req }) => res.send(data) | ||
}) | ||
``` | ||
The stale ttl maximizes your cache HITs, allowing you to serve a no fresh cache copy _while_ doing revalidation on the background. | ||
```bash | ||
curl https://myserver.dev/user # MISS (first access) | ||
curl https://myserver.dev/user # HIT (served from cache) | ||
curl https://myserver.dev/user # STALE (23 hours later, background revalidation) | ||
curl https://myserver.dev/user?force=true # HIT (fresh cache copy for the next 24 hours) | ||
``` | ||
The library provides enough good sensible defaults for most common scenarios and you can tune these values based on your use case. | ||
## API | ||
@@ -169,9 +195,2 @@ | ||
##### getKey | ||
Type: `function`<br/> | ||
Default: `({ req }) => normalizeUrl(req.url)` | ||
It determinates how the cache key should be computed, receiving `req, res` as input. | ||
The method will received `({ req, res })` and it should be returns: | ||
@@ -187,2 +206,9 @@ | ||
##### key | ||
Type: `function`<br/> | ||
Default: `({ req }) => req.url)` | ||
It determinates how the cache key should be computed, receiving `req, res` as input. | ||
##### send | ||
@@ -189,0 +215,0 @@ |
@@ -17,3 +17,3 @@ 'use strict' | ||
get, | ||
getKey = require('./util').getKey, | ||
key: getKey = require('./util').key, | ||
send, | ||
@@ -20,0 +20,0 @@ staleTtl: rawStaleTtl = 3600000, |
@@ -15,3 +15,3 @@ 'use strict' | ||
const getKey = ({ req }, { bypassQueryParameter }) => { | ||
const key = ({ req }, { bypassQueryParameter }) => { | ||
const urlObj = new URL(req.url, 'http://localhost:8080') | ||
@@ -65,3 +65,3 @@ const OMIT_KEYS = [bypassQueryParameter, /^utm_\w+/i] | ||
module.exports = { | ||
getKey, | ||
key, | ||
hasQueryParameter, | ||
@@ -68,0 +68,0 @@ isFunction, |
38701
338