新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
關(guān)于redigo中PubSub的一點小坑分析-創(chuàng)新互聯(lián)
前言
最近在用 golang 做一些 redis 相關(guān)的操作,選用了 redigo 這個第三方庫。然后在使用 Pub/Sub 的時候,卻發(fā)現(xiàn)了一個小坑……
Redis Client
首先,我們來初始化一個帶連接池的 Redis Client:
import ( "github.com/gomodule/redigo/redis" ) type RedisClient struct { pool *redis.Pool } func NewRedisClient(addr string, db int, passwd string) *RedisClient { pool := &redis.Pool{ MaxIdle: 10, IdleTimeout: 300 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", addr, redis.DialPassword(passwd), redis.DialDatabase(db)) if err != nil { return nil, err } return c, nil }, TestOnBorrow: func(c redis.Conn, t time.Time) error { if time.Since(t) < time.Minute { return nil } _, err := c.Do("PING") return err }, } log.Printf("new redis pool at %s", addr) client := &RedisClient{ pool: pool, } return client }
文章題目:關(guān)于redigo中PubSub的一點小坑分析-創(chuàng)新互聯(lián)
當前URL:http://www.dlmjj.cn/article/dhjhej.html