Skip to content

Commit

Permalink
feat: rename getKey into key
Browse files Browse the repository at this point in the history
in order to be aligned with `@keyvhq/memomize` API.
  • Loading branch information
Kikobeats committed Aug 19, 2021
1 parent e7874c6 commit 5ae0a1f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ curl https://myserver.dev/user?force=true # BYPASS (skip cache copy)

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

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

### cacheableResponse([options])
Expand Down Expand Up @@ -165,13 +191,6 @@ async function get ({ req, res }) {
}
```

##### 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:

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

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

##### key

Type: `function`<br/>
Default: `({ req }) => req.url)`

It determinates how the cache key should be computed, receiving `req, res` as input.

##### send

_Required_<br/>
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const cacheableResponse = ({
cache = new Keyv({ namespace: 'ssr' }),
compress: enableCompression = false,
get,
getKey = require('./util').getKey,
key: getKey = require('./util').key,
send,
staleTtl: rawStaleTtl = 3600000,
ttl: rawTtl = 86400000,
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const hasQueryParameter = (req, key) => {
return Boolean(req.query ? req.query[key] : parse(req.url.split('?')[1])[key])
}

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

module.exports = {
getKey,
key,
hasQueryParameter,
isFunction,
setHeaders,
Expand Down
12 changes: 6 additions & 6 deletions test/util/get-key.js → test/util/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const test = require('ava')

const { getKey } = require('../../src/util')
const { key } = require('../../src/util')

test('default getKey dedupe requests', t => {
test('default key dedupe requests', t => {
t.is(
getKey(
key(
{
req: {
url: '/kikobeats?foo=bar&force'
Expand All @@ -17,7 +17,7 @@ test('default getKey dedupe requests', t => {
'/kikobeats?foo=bar'
)
t.is(
getKey(
key(
{
req: {
url: '/kikobeats?foo=bar&force=true'
Expand All @@ -28,7 +28,7 @@ test('default getKey dedupe requests', t => {
'/kikobeats?foo=bar'
)
t.is(
getKey(
key(
{
req: {
url: '/kikobeats?foo=bar&bypass=true'
Expand All @@ -39,7 +39,7 @@ test('default getKey dedupe requests', t => {
'/kikobeats?foo=bar'
)
t.is(
getKey(
key(
{
req: {
url: '/kikobeats?foo=bar&bypass=true&utm_source=twitter'
Expand Down

0 comments on commit 5ae0a1f

Please sign in to comment.