Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stale support #80

Merged
merged 5 commits into from
Aug 12, 2021
Merged
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
94 changes: 47 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const cacheableResponse = require('cacheable-response')
const ssrCache = cacheableResponse({
get: ({ req, res }) => ({
data: doSomething(req),
ttl: 7200000 // 2 hours
ttl: 86400000 // 24 hours
}),
send: ({ data, res, req }) => res.send(data)
})
Expand Down Expand Up @@ -136,33 +136,6 @@ The library delegates in [keyv](https://github.com/lukechilds/keyv), a tiny key

If you don't specify it, a memory cache will be used.

##### ttl

Type: `number`<br/>
Default: `7200000`

Number of milliseconds a cache response is considered valid.

After this period of time, the cache response should be refreshed.

This value can be specified as well providing it as part of [`.get`](#get) output.

If you don't provide one, this be used as fallback for avoid keep things into cache forever.

##### serialize

Type: `function`<br/>
Default: `JSON.stringify`

Set the serializer method to be used before compress.

##### deserialize

Type: `function`<br/>
Default: `JSON.parse`

Set the deserialize method to be used after decompress.

##### compress

Type: `boolean`<br/>
Expand All @@ -176,24 +149,6 @@ If you enable it, you need to an additional `iltorb` package:
npm install iltorb
```

##### revalidate

Type: `function`|`number`<br/>
Default: `ttl => ttl / 24`

Number of milliseconds that indicates grace period after response cache expiration for refreshing it in the background. the latency of the refresh is hidden from the user.

You can provide a function, it will receive [`ttl`](#ttl) as first parameter or a fixed value.

The value will be associated with [`stale-while-revalidate`](https://www.mnot.net/blog/2014/06/01/chrome_and_stale-while-revalidate) directive.

##### getKey

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

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

##### get

_Required_<br/>
Expand All @@ -204,12 +159,19 @@ The method to be called for creating a fresh cacheable response associated with
```js
async function get ({ req, res }) {
const data = doSomething(req, res)
const ttl = 7200000 // 2 hours
const ttl = 86400000 // 24 hours
const headers = { userAgent: 'cacheable-response' }
return { data, ttl, headers }
}
```

##### 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 @@ -236,6 +198,44 @@ async function send ({ req, res, data, headers }) {

It will receive `({ req, res, data, ...props })` being `props` any other data supplied to `.get`.

##### staleTtl

Type: `number`|`boolean`<br/>
Default: `3600000`

Number of milliseconds that indicates grace period after response cache expiration for refreshing it in the background. the latency of the refresh is hidden from the user.

The value will be associated with [`stale-while-revalidate`](https://www.mnot.net/blog/2014/06/01/chrome_and_stale-while-revalidate) directive.

You can pass a `false` to disable it.

##### ttl

Type: `number`<br/>
Default: `86400000`

Number of milliseconds a cache response is considered valid.

After this period of time, the cache response should be refreshed.

This value can be specified as well providing it as part of [`.get`](#get) output.

If you don't provide one, this be used as fallback for avoid keep things into cache forever.

##### serialize

Type: `function`<br/>
Default: `JSON.stringify`

Set the serializer method to be used before compress.

##### deserialize

Type: `function`<br/>
Default: `JSON.parse`

Set the deserialize method to be used after decompress.

## Pro-tip: Distributed cache with CloudFlare™️

> This content is not sponsored; Just I consider CloudFlare is doing a good job offering a cache layer as part of their free tier.
Expand Down
136 changes: 0 additions & 136 deletions index.js

This file was deleted.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "An HTTP compliant route path middleware for serving cache response with invalidation support.",
"homepage": "https://nicedoc.io/Kikobeats/cacheable-response",
"version": "2.3.0",
"main": "index.js",
"main": "src/index.js",
"author": {
"email": "josefrancisco.verdu@gmail.com",
"name": "Kiko Beats",
Expand Down Expand Up @@ -55,10 +55,10 @@
],
"dependencies": {
"@keyvhq/core": "~1.1.1",
"@keyvhq/memoize": "~1.2.0",
"compress-brotli": "~1.3.0",
"debug-logfmt": "~1.0.4",
"etag": "~1.8.1",
"normalize-url": "~6.1.0",
"pretty-ms": "~7.0.1"
},
"devDependencies": {
Expand All @@ -67,6 +67,7 @@
"ava": "latest",
"ci-publish": "latest",
"conventional-github-releaser": "latest",
"delay": "latest",
"finepack": "latest",
"git-authors-cli": "latest",
"got": "latest",
Expand Down Expand Up @@ -105,8 +106,8 @@
"license": "MIT",
"ava": {
"files": [
"!test/util.js",
"test/**/*.js"
"test/**/*.js",
"!test/helpers.js"
]
},
"commitlint": {
Expand Down
Loading