go使用redis消息队列
import "github.com/gomodule/redigo/redis"
var (
RedisPools *RedisPool
)
type Job struct {
Class string `json:"Class"`
Args string `json:"Args"`
}
//redis连接池
type RedisPool struct {
pool *redis.Pool
}
func NewRedisPool() {
server := “127.0.0.1:6379”
password := “123456”
pool := &redis.Pool{
MaxIdle: 3, //最大空闲连接数
IdleTimeout: 240 * time.Second, //最大空闲连接时间
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", server)
if err != nil {
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
RedisPools = &RedisPool{pool}
}
#获取队列内容 队列名key
func (r *Redis