Skip to content

Commit

Permalink
feat: expose .getKey method
Browse files Browse the repository at this point in the history
closes #11
closes #22
  • Loading branch information
Kikobeats committed May 5, 2019
1 parent 1c7fd38 commit 3603e4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ curl https://myserver.dev/user?force=true # MISS (forcing invalidation)

##### cache

Type: `boolean`<br>
Type: `boolean`<br/>
Default: `new Keyv({ namespace: 'ssr' })`

The cache instance used for backed your pre-calculated server side response copies.
Expand All @@ -133,7 +133,7 @@ If you don't specify it, a memory cache will be used.

##### ttl

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

Number of milliseconds a cache response is considered valid.
Expand All @@ -146,21 +146,21 @@ If you don't provide one, this be used as fallback for avoid keep things into ca

##### serialize

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

Set the serializer method to be used before compress.

##### deserialize

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

Set the deserialize method to be used after decompress.

##### compress

Type: `boolean`<br>
Type: `boolean`<br/>
Default: `false`

Enable compress/decompress data using brotli compression format.
Expand All @@ -173,7 +173,7 @@ npm install iltorb

##### revalidate

Type: `function`|`number`<br>
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.
Expand All @@ -182,10 +182,17 @@ You can provide a function, it will receive [`ttl`](#ttl) as first parameter or

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 using `req` as input.

##### get

_Required_<br>
Type: `function`<br>
_Required_<br/>
Type: `function`<br/>

The method to be called for creating a fresh cacheable response associated with the current route path.

Expand All @@ -207,8 +214,8 @@ Any other property can be specified and will passed to `.send`.

##### send

_Required_<br>
Type: `function`<br>
_Required_<br/>
Type: `function`<br/>

The method used to determinate how the content should be rendered.

Expand Down Expand Up @@ -289,7 +296,7 @@ You can have a better overview of the percentage of success by looking your Clou

## License

**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.<br>
**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.<br/>
Authored and maintained by Kiko Beats with help from [contributors](https://github.com/Kikobeats/cacheable-response/contributors).

> [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const getEtag = require('etag')
const { URL } = require('url')
const Keyv = require('keyv')

const getKey = url => {
const _getKey = req => {
const url = urlResolve('http://localhost', req.url)
const { origin } = new URL(url)
const baseKey = normalizeUrl(url, {
removeQueryParameters: [/^utm_\w+/i, 'force', 'filter', 'ref']
Expand Down Expand Up @@ -43,6 +44,7 @@ const createSetHeaders = ({ revalidate }) => {
module.exports = ({
cache = new Keyv({ namespace: 'ssr' }),
compress: enableCompression = false,
getKey = _getKey,
get,
send,
revalidate = ttl => ttl / 24,
Expand All @@ -65,7 +67,7 @@ module.exports = ({
const hasForce = Boolean(
req.query ? req.query.force : parse(req.url.split('?')[1]).force
)
const key = getKey(urlResolve('http://localhost', req.url))
const key = getKey(req)
const cachedResult = await decompress(await cache.get(key))
const isHit = !hasForce && cachedResult !== undefined

Expand Down Expand Up @@ -98,4 +100,4 @@ module.exports = ({
}
}

module.exports.getKey = getKey
module.exports.getKey = _getKey

0 comments on commit 3603e4d

Please sign in to comment.