Skip to content

Commit

Permalink
chore: readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood committed Aug 27, 2023
1 parent 28470ef commit bf34078
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm i @fastify/redis

Add it to your project with `register` and you are done!

### Create a new Redis Client
### Create a new Redis/Dragonfly Client

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.

Expand Down Expand Up @@ -79,6 +79,48 @@ fastify.listen({ port: 3000 }, err => {
})
```

### Accessing the Dragonfly Client

Once you have registered your plugin, you can access the Redis client via `fastify.dragonfly`.

The client is automatically closed when the fastify instance is closed.

```js
'use strict'

const Fastify = require('fastify')
const fastifyRedis = require('@fastify/redis')

const fastify = Fastify({ logger: true })

fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: 'your strong password here',
port: 6379, // Dragonfly port
family: 4 // 4 (IPv4) or 6 (IPv6),
isDragonfly: true
})

fastify.get('/foo', (req, reply) => {
const { dragonfly } = fastify
dragonfly.get(req.query.key, (err, val) => {
reply.send(err || val)
})
})

fastify.post('/foo', (req, reply) => {
const { dragonfly } = fastify
dragonfly.set(req.body.key, req.body.value, (err) => {
reply.send(err || { status: 'ok' })
})
})

fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```

### Using an existing Redis client

You may also supply an existing *Redis* client instance by passing an options
Expand Down

0 comments on commit bf34078

Please sign in to comment.