1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package main
- import (
- "fmt"
- rd "git.aionnect.com/aionnect/go-common/utils/redis"
- "github.com/gomodule/redigo/redis"
- "github.com/spf13/viper"
- "time"
- )
- func main() {
- // 配置
- viper.SetDefault("redis.host", "127.0.0.1:6379")
- //viper.SetDefault("redis.host","192.168.101.161:6379")
- //viper.SetDefault("redis.nodes", []string{"192.168.101.161:6379", "192.168.101.125:6379", "192.168.101.170:6379"})
- // 初始化
- q := rd.NewRedisQueue("myList", "backList", 3000, 5*time.Second)
- q.Clean()
- q.Recycle()
- // 消费
- for i := 0; i < 4; i++ {
- go func(idx int, q *rd.Queue) {
- q.Pop(func(reply interface{}) {
- content, _ := redis.String(reply, nil)
- fmt.Printf("Receiver %d get:%+v\n", idx, content)
- })
- }(i, q)
- }
- // 生产
- go func(q *rd.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
- }
|