Skip to content

Commit

Permalink
Merge 0c4d480 into 0bd8a4e
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 12, 2021
2 parents 0bd8a4e + 0c4d480 commit 749583e
Show file tree
Hide file tree
Showing 13 changed files with 657 additions and 481 deletions.
94 changes: 47 additions & 47 deletions README.md
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
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

0 comments on commit 749583e

Please sign in to comment.