func TestPushed(t *testing.T) { pc := dialt(t) defer pc.Close() nc, err := net.Dial("tcp", ":6379") if err != nil { t.Fatal(err) } defer nc.Close() nc.SetReadDeadline(time.Now().Add(4 * time.Second)) c := redis.PubSubConn{Conn: redis.NewConn(nc, 0, 0)} c.Subscribe("c1") expectPushed(t, c, "Subscribe(c1)", redis.Subscription{Kind: "subscribe", Channel: "c1", Count: 1}) c.Subscribe("c2") expectPushed(t, c, "Subscribe(c2)", redis.Subscription{Kind: "subscribe", Channel: "c2", Count: 2}) c.PSubscribe("p1") expectPushed(t, c, "PSubscribe(p1)", redis.Subscription{Kind: "psubscribe", Channel: "p1", Count: 3}) c.PSubscribe("p2") expectPushed(t, c, "PSubscribe(p2)", redis.Subscription{Kind: "psubscribe", Channel: "p2", Count: 4}) c.PUnsubscribe() expectPushed(t, c, "Punsubscribe(p1)", redis.Subscription{Kind: "punsubscribe", Channel: "p1", Count: 3}) expectPushed(t, c, "Punsubscribe()", redis.Subscription{Kind: "punsubscribe", Channel: "p2", Count: 2}) pc.Do("PUBLISH", "c1", "hello") expectPushed(t, c, "PUBLISH c1 hello", redis.Message{Channel: "c1", Data: []byte("hello")}) }
func newConnectionPool(connector Connector, size int) *redis.Pool { return redis.NewPool(func() (redis.Conn, error) { conn, err := connector.Connect() if err != nil { return nil, err } return redis.NewConn(conn, 0, 0), nil }, size) }
// NewBroker returns a new *Broker. func NewBroker(cfg BrokerConfig) (*Broker, error) { conn, err := net.Dial("tcp", cfg.Hostport) if err != nil { return nil, err } return &Broker{ rc: redis.NewConn(conn, cfg.Timeout, cfg.Timeout), registry: make(map[string]Job), dq: cfg.DefaultQueue, }, nil }
func GetKeyByCache(key string) (value string, err error) { netConn, err := net.Dial("tcp", "127.0.0.1:6379") if err != nil { return } redisConn := redis.NewConn(netConn, 200*time.Millisecond, 200*time.Millisecond) value, err = redis.String(redisConn.Do("GET", key)) if err != nil { return } return }
func main() { fmt.Printf("==============================================================\n") redisAddr := "localost:7003" conn, err := net.Dial("tcp", redisAddr) if err != nil { fmt.Printf("%s: conn redis :%s\n", setColour("WARNING", "red"), redisAddr) } c := redis.NewConn(conn, 2e7, 2e7) // get all keys * // dump value save to hd c.Do() _ = c fmt.Printf("==============================================================\n") }
func SetCacheByKey(key string, value string) (err error) { netConn, err := net.Dial("tcp", "127.0.0.1:6379") if err != nil { return err } redisConn := redis.NewConn(netConn, 200*time.Millisecond, 200*time.Millisecond) ok, err := redis.Bool(redisConn.Do("SET", key, value)) if err != nil { return err } if !ok { return fmt.Errorf("set not ok") } return err }
func main() { const ( HeartBeatTime = 30 ) nc, err := net.Dial("tcp", ":6969") if err != nil { fmt.Println(err) } defer nc.Close() // nc.SetReadDeadline(time.Now().Add(4 * time.Second)) c := redis.NewConn(nc, 0, 0) //订阅频道 c.Send("sub", "Channel", HeartBeatTime) c.Flush() // 定时请求心跳 ticker := time.NewTicker(time.Second * HeartBeatTime) go func() { for t := range ticker.C { nc.Write([]byte("h")) fmt.Println("Heartbeat send", t) } }() for { reply, err := c.Receive() if err != nil { fmt.Println(err) } if reply == "h" { fmt.Println("Heartbeat received") } else if reply != nil { reply_str, err := redis.String(reply, err) fmt.Printf("%#v\n", reply_str) if err != nil { fmt.Println(err) } } } }
func (c *client) Connect(to time.Duration) error { debugf("connect") err := c.Client.Connect() if err != nil { return err } conn := redis.NewConn(c.Client, to, to) defer func() { if err != nil { conn.Close() } }() if err = initRedisConn(conn, c.password, c.db); err == nil { c.publish, err = makePublish(conn, c.key, c.dataType) } return err }
func openRedisConn(target string) redigo.Conn { return redigo.NewConn(openNetConn(target), 0, 0) }
func GetRedisConn(redisAddr string) (redisConn redis.Conn) { conn, err := net.Dial("tcp", redisAddr) CheckErr(err, "conn redis error") redisConn = redis.NewConn(conn, 200*time.Millisecond, 200*time.Millisecond) return redisConn }
func redisPipeConn() (redis.Conn, net.Conn) { nConn, c := net.Pipe() rConn := redis.NewConn(c, 0, 0) return rConn, nConn }