diff --git a/Readme.md b/Readme.md index 10ae53a..723df37 100644 --- a/Readme.md +++ b/Readme.md @@ -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 @@ -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) => { @@ -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='` 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)` diff --git a/package.json b/package.json index af76d43..ff487a8 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/plugin.js b/plugin.js index 8da25a6..ec7da5e 100644 --- a/plugin.js +++ b/plugin.js @@ -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' }