12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package redis
- import (
- "fmt"
- "github.com/gomodule/redigo/redis"
- "github.com/spf13/viper"
- "testing"
- "time"
- )
- func TestRedisQueue(t *testing.T) {
- // 配置
- viper.SetDefault("redis.host", "127.0.0.1:6379") // single node or master/slave
- //viper.SetDefault("redis.nodes", []string{"192.168.101.68:6379"}) // cluster or master/slave with sentinel
- //viper.SetDefault("redis.master", "mymaster") // master name config for sentinel
- // 初始化
- q := NewRedisQueue("myList", "backList", 3000, 5*time.Second)
- q.IsIdempotent = true
- q.Clean()
- q.Recycle()
- // 消费
- for i := 0; i < 4; i++ {
- go func(idx int, q *Queue) {
- q.Pop(func(reply interface{}) error {
- content, _ := redis.String(reply, nil)
- fmt.Printf("Receiver %d get:%+v\n", idx, content)
- return nil
- })
- }(i, q)
- }
- // 生产
- go func(q *Queue) {
- for i := 0; i < 20; i++ {
- q.Push(fmt.Sprintf("Message %d", i+1))
- time.Sleep(1 * time.Second)
- }
- }(q)
- quit := make(chan bool)
- <-quit
- }
|