Skip to content
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
39 changes: 23 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ contexts.
In addition to providing header manipulation, the plugin also decorates the
server instance with an object that can be used for caching items. **Note:**
the default cache should not be used in a "production" environment. It is
an LRU, in-memory, cache that is capped at 100,000 items. It is *highly*
an LRU, in-memory cache that is capped at 100,000 items. It is *highly*
recommended that a full featured cache object be supplied, e.g.
[catbox-redis][catbox-redis].
[abstract-cache-redis][acache-redis].

[catbox-redis]: https://www.npmjs.com/package/catbox-redis
[acache-redis]: https://www.npmjs.com/package/abstract-cache-redis

## Example

Expand Down Expand Up @@ -48,15 +48,23 @@ fastify.listen(3000, (err) => {

This example shows how to register the plugin such that it only provides
a server-local cache. It will not set any cache control headers on responses.
It also shows how to retain a reference to the cache object so that it can
be re-used.

```js
const fastify = require('fastify')
const fastifyCaching = require('fastify-caching')
const redis = require('ioredis')({host: '127.0.0.1'})
const abcache = require('abstract-cache')({
useAwait: false,
driver: {
name: 'abstract-cache-redis', // must be installed via `npm install`
options: {client: redis}
}
})

fastify.register(
fastifyCaching,
(err) => { if (err) throw err }
)
const fastify = require('fastify')()
fastify
.register(require('fastify-redis'), {client: redis})
.register(require('fastify-caching'), {cache: abcache})

fastify.get('/', (req, reply) => {
fastify.cache.set('hello', {hello: 'world'}, 10000, (err) => {
Expand Down Expand Up @@ -90,15 +98,14 @@ for a *cache-response-directive* as defined by RFC 2616.
+ `expiresIn` (Default: `undefined`): a value, in seconds, for the *max-age* the
resource may be cached. When this is set, and `privacy` is not set to `no-cache`,
then `', max-age=<value>'` will be appended to the `cache-control` header.
+ `cache` (Default: instance of [@jsumners/memcache][jsmemcache]): a
[Catbox (v7)][catbox] protocol compliant cache object. Note: the plugin
requires a cache instance to properly support the ETag mechanism. Therefore,
if a falsy value is supplied the default will be used.
+ `cache` (Default: `abstract-cache.memclient`): an [abstract-cache][acache]
protocol compliant cache object. Note: the plugin requires a cache instance to
properly support the ETag mechanism. Therefore, if a falsy value is supplied
the default will be used.
+ `cacheSegment` (Default: `'fastify-caching'`): segment identifier to use when
communicating with the supplied Catbox cache.
communicating with the cache.

[jsmemcache]: https://www.npmjs.com/package/@jsumners/memcache
[catbox]: https://github.com/hapijs/catbox/tree/v7.1.5
[acache]: https://www.npmjs.com/package/abstract-cache

### `reply.etag(string, number)`

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
},
"homepage": "https://github.com/fastify/fastify-caching#readme",
"devDependencies": {
"fastify": "^0.33.0",
"fastify": "^0.35.2",
"pre-commit": "^1.2.2",
"snazzy": "^7.0.0",
"standard": "^10.0.3",
"tap": "^10.7.3"
},
"dependencies": {
"@jsumners/memcache": "^2.1.0",
"abstract-cache": "^1.0.0",
"fastify-plugin": "^0.1.1",
"uid-safe": "^2.1.5"
}
Expand Down
2 changes: 1 addition & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const uidSafe = require('uid-safe')
const defaultOptions = {
expiresIn: undefined,
privacy: undefined,
cache: require('@jsumners/memcache')('fastify-caching'),
cache: require('abstract-cache')(),
cacheSegment: 'fastify-caching'
}

Expand Down