|
1 | 1 | # fastify-response-caching
|
2 | 2 |
|
3 | 3 | 
|
| 4 | + |
| 5 | +*fastify-response-caching* is a plugin for the [Fastify](http://fastify.io/) framework |
| 6 | +that provides mechanisms for caching response to reduce the server workload. |
| 7 | + |
| 8 | +By default, this plugin implements caching by request URL (includes all query parameters) |
| 9 | +with the caching time (TTL) is 1 seconds. Besides, this plugin also supports additional caching condition |
| 10 | +such as request headers. |
| 11 | + |
| 12 | +## Example |
| 13 | + |
| 14 | +This example shows using the plugin to cache response with default options. |
| 15 | + |
| 16 | +```js |
| 17 | +const fastify = require('fastify') |
| 18 | +const fastifyResponseCaching = require('fastify-response-caching') |
| 19 | + |
| 20 | +fastify.register(fastifyResponseCaching) |
| 21 | +``` |
| 22 | + |
| 23 | +This example shows using the plugin to cache response with customized caching time. |
| 24 | + |
| 25 | +```js |
| 26 | +const fastify = require('fastify') |
| 27 | +const fastifyResponseCaching = require('fastify-response-caching') |
| 28 | + |
| 29 | +fastify.register(fastifyResponseCaching, {ttl: 5000}) |
| 30 | +``` |
| 31 | + |
| 32 | +This example shows using the plugin to cache response with customized caching conditions. |
| 33 | + |
| 34 | +```js |
| 35 | +const fastify = require('fastify') |
| 36 | +const fastifyResponseCaching = require('fastify-response-caching') |
| 37 | + |
| 38 | +fastify.register(fastifyResponseCaching, {ttl: 5000, headers: ['x-request-agent']}) |
| 39 | +``` |
| 40 | + |
| 41 | +## API |
| 42 | + |
| 43 | +### Options |
| 44 | + |
| 45 | +*fastify-response-caching* accepts the options object: |
| 46 | + |
| 47 | +```js |
| 48 | +{ |
| 49 | + ttl: <Number> |
| 50 | + additionalCondition: { |
| 51 | + headers: <Array<String>> |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | ++ `ttl` (Default: `1000`): a value, in milliseconds, for the lifetime of the response cache. |
| 57 | ++ `additionalCondition` (Default: `undefined`): a configuration of additional condition for caching. |
| 58 | ++ `additionalCondition.headers` (Default: `[]`): a list of string, headers that you want to include in the caching condition. |
| 59 | + |
| 60 | +## License |
| 61 | + |
| 62 | +[MIT License](LICENSE) |
0 commit comments