Skip to content

Commit 0f38c64

Browse files
authored
docs(readme): create options, add more headings
1 parent fb463e1 commit 0f38c64

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,42 @@
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
21+
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.
2323

24+
```js
2425
const fastify = require('fastify')()
2526

27+
// minimum options
2628
fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
27-
// or
28-
fastify.register(require('fastify-redis'), { url: 'redis://127.0.0.1', /* other redis options */ })
2929

30+
// OR with more options
31+
fastify.register(require('fastify-redis'), {
32+
host: '127.0.0.1',
33+
password: '***',
34+
port: 6379, // Redis port
35+
family: 4 // 4 (IPv4) or 6 (IPv6)
36+
})
37+
```
38+
39+
### Accessing the Redis Client
40+
41+
Once you're registered your plugin, you can access the Redis client via `fastify.redis`.
42+
43+
The client is automatically closed when the fastify instance is closed.
44+
45+
```js
3046
fastify.get('/foo', (req, reply) => {
3147
const { redis } = fastify
3248
redis.get(req.query.key, (err, val) => {
@@ -47,6 +63,8 @@ fastify.listen(3000, err => {
4763
})
4864
```
4965

66+
### Using an existing Redis client
67+
5068
You may also supply an existing *Redis* client instance by passing an options
5169
object with the `client` property set to the instance. In this case,
5270
the client is not automatically closed when the Fastify instance is

0 commit comments

Comments
 (0)