39 lines
653 B
Go
39 lines
653 B
Go
package redisMq
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
type Producer struct {
|
|
client *redis.Client
|
|
opts *ProducerOptions
|
|
ctx context.Context
|
|
}
|
|
|
|
func (p *Producer) SendMsg(topic string, key, val string) (string, error) {
|
|
add := p.client.XAdd(p.ctx, &redis.XAddArgs{
|
|
Stream: topic,
|
|
MaxLen: p.opts.msgQueueLen,
|
|
Values: map[string]interface{}{
|
|
key: val,
|
|
},
|
|
})
|
|
|
|
return add.Result()
|
|
}
|
|
|
|
func NewProducer(client *redis.Client, opts ...ProducerOption) *Producer {
|
|
p := &Producer{
|
|
client: client,
|
|
opts: &ProducerOptions{},
|
|
ctx: context.Background(),
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(p.opts)
|
|
}
|
|
|
|
return p
|
|
}
|