redis.go 916 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "fmt"
  4. rd "git.aionnect.com/aionnect/go-common/utils/redis"
  5. "github.com/gomodule/redigo/redis"
  6. "github.com/spf13/viper"
  7. "time"
  8. )
  9. func main() {
  10. // 配置
  11. viper.SetDefault("redis.host", "127.0.0.1:6379")
  12. //viper.SetDefault("redis.host","192.168.101.161:6379")
  13. //viper.SetDefault("redis.nodes", []string{"192.168.101.161:6379", "192.168.101.125:6379", "192.168.101.170:6379"})
  14. // 初始化
  15. q := rd.NewRedisQueue("myList", "backList", 3000, 5*time.Second)
  16. q.Clean()
  17. q.Recycle()
  18. // 消费
  19. for i := 0; i < 4; i++ {
  20. go func(idx int, q *rd.Queue) {
  21. q.Pop(func(reply interface{}) {
  22. content, _ := redis.String(reply, nil)
  23. fmt.Printf("Receiver %d get:%+v\n", idx, content)
  24. })
  25. }(i, q)
  26. }
  27. // 生产
  28. go func(q *rd.Queue) {
  29. for i := 0; i < 20; i++ {
  30. q.Push(fmt.Sprintf("Message %d", i+1))
  31. time.Sleep(1 * time.Second)
  32. }
  33. }(q)
  34. quit := make(chan bool)
  35. <-quit
  36. }