Skip to content

Commit 727a27e

Browse files
authored
docs(readme): create options, add more headings
2 parents fb463e1 + f86ed40 commit 727a27e

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

README.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,59 @@
77

88
Fastify Redis connection plugin; with this you can share the same Redis connection in every part of your server.
99

10-
Under the hood [ioredis](https://github.com/luin/ioredis) is used as client, the ``options`` that you pass to `register` will be passed to the Redis client.
11-
1210
## Install
11+
1312
```
1413
npm i fastify-redis --save
1514
```
15+
1616
## Usage
17+
1718
Add it to your project with `register` and you are done!
18-
You can access the *Redis* client via `fastify.redis`. The client is
19-
automatically closed when the fastify instance is closed.
2019

21-
```js
22-
'use strict'
20+
### Create a new Redis Client
2321

22+
Under the hood [ioredis](https://github.com/luin/ioredis) is used as client, the ``options`` that you pass to `register` will be passed to the Redis client.
23+
24+
```js
2425
const fastify = require('fastify')()
2526

27+
// create by specifying host
2628
fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
27-
// or
29+
30+
// OR by specifying Redis URL
2831
fastify.register(require('fastify-redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
2932

33+
// OR with more options
34+
fastify.register(require('fastify-redis'), {
35+
host: '127.0.0.1',
36+
password: '***',
37+
port: 6379, // Redis port
38+
family: 4 // 4 (IPv4) or 6 (IPv6)
39+
})
40+
```
41+
42+
### Accessing the Redis Client
43+
44+
Once you have registered your plugin, you can access the Redis client via `fastify.redis`.
45+
46+
The client is automatically closed when the fastify instance is closed.
47+
48+
```js
49+
'use strict'
50+
51+
const Fastify = require('fastify')
52+
const fastifyRedis = require('fastify-redis')
53+
54+
const fastify = Fastify({ logger: true })
55+
56+
fastify.register(fastifyRedis, {
57+
host: '127.0.0.1',
58+
password: 'your strong password here',
59+
port: 6379, // Redis port
60+
family: 4 // 4 (IPv4) or 6 (IPv6)
61+
})
62+
3063
fastify.get('/foo', (req, reply) => {
3164
const { redis } = fastify
3265
redis.get(req.query.key, (err, val) => {
@@ -47,6 +80,8 @@ fastify.listen(3000, err => {
4780
})
4881
```
4982

83+
### Using an existing Redis client
84+
5085
You may also supply an existing *Redis* client instance by passing an options
5186
object with the `client` property set to the instance. In this case,
5287
the client is not automatically closed when the Fastify instance is

0 commit comments

Comments
 (0)