Thứ Tư, 14 tháng 4, 2021

Redis trong AdonisJS




## Redis
1. Cài đặt
> adonis install @adonisjs/redis

2. Đăng kí RedisProvider
Chỉnh sửa file `start/app.js`

const providers = [
'@adonisjs/redis/providers/RedisProvider'
]

3. Cấu hình redis
Chỉnh sửa ở `config/redis.js`


## Basic Use
File `app/Controllers/Http/UserController.js`
<!-->
'use strict'

const Redis = use('Redis')
const User = use('App/Models/User')

class UserController {

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
}
}
<-->


## Redis Commands
https://redis.io/commands
<!-->
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)
<-->


## Pub/Sub
Redis Provider hỗ trợ publish/subscribe (pub/sub) để chia sẻ tin nhắn trên cùng một server hoặc băng qua nhiều server
Sử dụng cho các dịch vụ tin nhắn, chat message.
khi các thành viên cùng tham gia vào 1 kênh(`subscribe`),
thì khi một thành viên bất kì gửi tin nhắn(`publish`) thì các thành viên khác trong kênh đều nhận
được message ngay.

1. Đăng kí một kênh
start/redis.js
<!-->
'use strict'

const Redis = use('Redis')

Redis.subscribe('music', async (track) => {
console.log('received track', track)
})
<-->

Tạo một file `start/redis.js` nếu nó không tồn tại và load nó ở file `server.js`:
> new Ignitor(require('@adonisjs/fold')).preLoad('start/redis')

2. Khi một kênh đã được đăng kí thì có thể sử dụng như bên dưới
> const Redis = use('Redis')
> Redis.publish('music', track)

3. Một số method sẵn có
3. a) - `subscribe(channel, listener)`
<!-->
Redis.subscribe('music', (track) {
console.log(track)
})
<-->

Bạn cũng có thể thêm vào theo dạng
> Redis.subscribe('music', 'Music.newTrack')

Và tạo mới file app/Listeners/Music.js để xử lý
<!-->
'use strict'

const Music = exports = module.exports = {}

Music.newTrack = (track) => {
console.log(track)
}
<-->

3. b) - `psubscribe(pattern, listener)`
Subscribe đến một mẫu(sử dụng dấu ?):
<!-->
Redis.psubscribe('h?llo', function (pattern, message, channel) {
})
<-->
==>
Redis.publish('hello')
Redis.publish('hallo')

3. c) - `publish(channel, message)`
Gửi tin nhắn đến 1 kênh đã đăng ký
<!-->
Redis.publish('music', JSON.stringify({
id: 1,
title: 'Love me like you do',
artist: 'Ellie goulding'
}))
<-->

3. d) - `unsubscribe(channel)`
Hủy đăng kí một kênh. Ex: Redis.unsubscribe('music')

3. e) - `punsubscribe(channel)`
> Redis.punsubscribe('h?llo')


## Thiết kế liên kết nhiều redis server
File `config/redis.js`
<!-->
module.exports = {
connection: 'local',

local: {
...
},

secondary: {
host: 'myhost.com',
port: 6379
}
}
<-->

1. Kết nối một redis connection
<!-->
await Redis.connection('secondary').get('users')
OR
// hold reference to connection
const secondaryConnection = Redis.connection('secondary')
await secondaryConnection.get('users')
<-->

2. Hủy bỏ kết nối
> await Redis.quit('primary')
> await Redis.quit(['primary', 'secondary'])



Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...