|
1 | 1 | # fastify-redis
|
2 |
| -Plugin to share a common Redis connection across Fastify. |
| 2 | + |
| 3 | +[](http://standardjs.com/) [](https://travis-ci.org/fastify/fastify-redis) |
| 4 | + |
| 5 | +Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server. |
| 6 | + |
| 7 | +Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the options that you pass to `register` will be passed to the Redis client. |
| 8 | + |
| 9 | +## Install |
| 10 | +``` |
| 11 | +npm i fastify-redis --save |
| 12 | +``` |
| 13 | +## Usage |
| 14 | +Add it to you project with `register` and you are done! |
| 15 | +You can access the *Redis* client via `fastify.redis`. |
| 16 | +```js |
| 17 | +const fastify = require('fastify') |
| 18 | + |
| 19 | +fastify.register(require('fastify-redis'), { |
| 20 | + host: '127.0.0.1' |
| 21 | +}, err => { |
| 22 | + if (err) throw err |
| 23 | +}) |
| 24 | + |
| 25 | +fastify.get('/foo', (req, reply) => { |
| 26 | + const { redis } = fastify.redis |
| 27 | + redis.get(req.query.key, (err, val) => { |
| 28 | + reply.send(err || val) |
| 29 | + }) |
| 30 | +}) |
| 31 | + |
| 32 | +fastify.post('/foo', (req, reply) => { |
| 33 | + const { redis } = fastify.redis |
| 34 | + redis.set(req.body.key, req.body.value, (err) => { |
| 35 | + reply.send(err || { status: 'ok' }) |
| 36 | + }) |
| 37 | +}) |
| 38 | + |
| 39 | +fastify.listen(3000, err => { |
| 40 | + if (err) throw err |
| 41 | + console.log(`server listening on ${fastify.server.address().port}`) |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +## Acknowledgements |
| 46 | + |
| 47 | +This project is kindly sponsored by: |
| 48 | +- [nearForm](http://nearform.com) |
| 49 | +- [LetzDoIt](http://www.letzdoitapp.com/) |
| 50 | + |
| 51 | +## License |
| 52 | + |
| 53 | +Licensed under [MIT](./LICENSE). |
0 commit comments