Skip to content

Commit 5ae0a1f

Browse files
committed
feat: rename getKey into key
in order to be aligned with `@keyvhq/memomize` API.
1 parent e7874c6 commit 5ae0a1f

4 files changed

Lines changed: 42 additions & 16 deletions

File tree

README.md

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,32 @@ curl https://myserver.dev/user?force=true # BYPASS (skip cache copy)
112112

113113
In that case, the `x-cache-status` will reflect a `'BYPASS'` value.
114114

115+
Additionally, you can configure a stale ttl:
116+
117+
```js
118+
const cacheableResponse = require('cacheable-response')
119+
120+
const ssrCache = cacheableResponse({
121+
get: ({ req, res }) => ({
122+
data: doSomething(req),
123+
ttl: 86400000, // 24 hours
124+
staleTtl: 3600000 // 1h
125+
}),
126+
send: ({ data, res, req }) => res.send(data)
127+
})
128+
```
129+
130+
The stale ttl maximizes your cache HITs, allowing you to serve a no fresh cache copy _while_ doing revalidation on the background.
131+
132+
```bash
133+
curl https://myserver.dev/user # MISS (first access)
134+
curl https://myserver.dev/user # HIT (served from cache)
135+
curl https://myserver.dev/user # STALE (23 hours later, background revalidation)
136+
curl https://myserver.dev/user?force=true # HIT (fresh cache copy for the next 24 hours)
137+
```
138+
139+
The library provides enough good sensible defaults for most common scenarios and you can tune these values based on your use case.
140+
115141
## API
116142

117143
### cacheableResponse([options])
@@ -165,13 +191,6 @@ async function get ({ req, res }) {
165191
}
166192
```
167193

168-
##### getKey
169-
170-
Type: `function`<br/>
171-
Default: `({ req }) => normalizeUrl(req.url)`
172-
173-
It determinates how the cache key should be computed, receiving `req, res` as input.
174-
175194
The method will received `({ req, res })` and it should be returns:
176195

177196
- **data** `object`|`string`: The content to be saved on the cache.
@@ -182,6 +201,13 @@ Any other property can be specified and will passed to `.send`.
182201

183202
In case you want to bypass the cache, preventing caching a value (e.g., when an error occurred), you should return `undefined` or `null`.
184203

204+
##### key
205+
206+
Type: `function`<br/>
207+
Default: `({ req }) => req.url)`
208+
209+
It determinates how the cache key should be computed, receiving `req, res` as input.
210+
185211
##### send
186212

187213
_Required_<br/>

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const cacheableResponse = ({
1414
cache = new Keyv({ namespace: 'ssr' }),
1515
compress: enableCompression = false,
1616
get,
17-
getKey = require('./util').getKey,
17+
key: getKey = require('./util').key,
1818
send,
1919
staleTtl: rawStaleTtl = 3600000,
2020
ttl: rawTtl = 86400000,

src/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const hasQueryParameter = (req, key) => {
1212
return Boolean(req.query ? req.query[key] : parse(req.url.split('?')[1])[key])
1313
}
1414

15-
const getKey = ({ req }, { bypassQueryParameter }) => {
15+
const key = ({ req }, { bypassQueryParameter }) => {
1616
const urlObj = new URL(req.url, 'http://localhost:8080')
1717
const OMIT_KEYS = [bypassQueryParameter, /^utm_\w+/i]
1818
const omitKeys = Array.from(urlObj.searchParams.keys()).reduce((acc, key) => {
@@ -61,7 +61,7 @@ const setHeaders = ({
6161
}
6262

6363
module.exports = {
64-
getKey,
64+
key,
6565
hasQueryParameter,
6666
isFunction,
6767
setHeaders,
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
const test = require('ava')
44

5-
const { getKey } = require('../../src/util')
5+
const { key } = require('../../src/util')
66

7-
test('default getKey dedupe requests', t => {
7+
test('default key dedupe requests', t => {
88
t.is(
9-
getKey(
9+
key(
1010
{
1111
req: {
1212
url: '/kikobeats?foo=bar&force'
@@ -17,7 +17,7 @@ test('default getKey dedupe requests', t => {
1717
'/kikobeats?foo=bar'
1818
)
1919
t.is(
20-
getKey(
20+
key(
2121
{
2222
req: {
2323
url: '/kikobeats?foo=bar&force=true'
@@ -28,7 +28,7 @@ test('default getKey dedupe requests', t => {
2828
'/kikobeats?foo=bar'
2929
)
3030
t.is(
31-
getKey(
31+
key(
3232
{
3333
req: {
3434
url: '/kikobeats?foo=bar&bypass=true'
@@ -39,7 +39,7 @@ test('default getKey dedupe requests', t => {
3939
'/kikobeats?foo=bar'
4040
)
4141
t.is(
42-
getKey(
42+
key(
4343
{
4444
req: {
4545
url: '/kikobeats?foo=bar&bypass=true&utm_source=twitter'

0 commit comments

Comments
 (0)