adonis install @adonisjs/redis
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
AdonisJs has first class support for redis built on top of ioredis with better pub/sub API.
Configuration, events API and all the methods from IoRedis are 100% supported. Make sure to read ioredis docs as well. |
Redis provider is not installed by default, so follow the given steps to install it from npm.
adonis install @adonisjs/redis
Next, register the provider inside start/app.js
file.
const providers = [
'@adonisjs/redis/providers/RedisProvider'
]
The configuration file is saved as config/redis.js
, make sure to define the connection settings before making use of the provider.
Let’s start with a basic example of caching users inside redis. The following example may not be the best way to cache data, but does give you an idea on how to make use of redis.
'use strict'
const Redis = use('Redis')
const User = use('App/Models/User')
class UsersController {
async index () {
const cachedUsers = await Redis.get('users')
if (cachedUsers) {
return JSON.parse(cachedUsers)
}
const users = await User.all()
await Redis.set('users', JSON.stringify(users))
return users
}
}
All redis commands are supported as Javascript functions. Also make sure to read ioredis documentation ( if required ).
const Redis = use('Redis')
const user = {
username: 'foo',
email: 'foo@bar.com'
}
// set user
await Redis.hmset('users', user.username, JSON.stringify(user))
// get user
const user = await Redis.hmget('users', user.username)
Redis has built-in support for Pub/Sub to share messages on same or across multiple servers. AdonisJs offers a clean API on top of redis pub/sub to subscribe to different events and act upon them.
All of the event listeners should go under start/events.js
file and so does the redis subscribers.
'use strict'
const Redis = use('Redis')
Redis.subscribe('music', async (track) => {
console.log('received track', track)
})
Once a subscriber has been registered, you can publish data to this channel from the same or different server.
const Redis = use('Redis')
Redis.publish('music', track)
Below is the list of methods to interact with the pub/sub layer of redis.
You can only have one subscriber for a given channel. |
Redis.subscribe('music', (track) {
console.log(track)
})
A listener can also be a file.method
reference from the app/Listeners
directory.
Redis.subscribe('music', 'Music.newTrack')
'use strict'
const Music = exports = module.exports = {}
Music.newTrack = (track) => {
console.log(track)
}
Subscribe to a pattern
Redis.psubscribe('h?llo', function (message, channel, pattern) {
})
Redis.publish('hello')
Redis.publish('hallo')
Publish message to a given channel
Redis.publish('music', JSON.stringify({
id: 1,
title: 'Love me like you do',
artist: 'Ellie goulding'
}))
You can define the configuration for multiple connections inside the config/redis.js
file, and you can use those connections by calling the connection method.
module.exports = {
connection: 'local',
local: {
...
},
secondary: {
host: 'myhost.com',
port: 6379
}
}
Use a different connection to make redis queries.
await Redis
.connection('secondary')
.get('users')
// hold reference to connection
const secondaryConnection = Redis.connection('secondary')
await secondaryConnection.get('users')
The redis provider creates a connections pool and reuses the existing connections. You can quit a connection by calling the quit method with a single or an array of connections.
await Redis.quit('secondary')