func PubSub() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered in PubSub", r) } }() c2, err := redis.Dial("tcp", redisIp) errHndlr(err) defer c2.Close() //authServer authE := c2.Cmd("auth", redisPassword) errHndlr(authE.Err) psc := pubsub.NewSubClient(c2) psr := psc.Subscribe("events") ppsr := psc.PSubscribe("EVENT:*") if ppsr.Err == nil { for { psr = psc.Receive() if psr.Err != nil { fmt.Println(psr.Err.Error()) break } list := strings.Split(psr.Message, ":") fmt.Println(list) if len(list) >= 8 { stenent := list[1] scompany := list[2] sclass := list[3] stype := list[4] scategory := list[5] sparam1 := list[6] sparam2 := list[7] ssession := list[8] itenet, _ := strconv.Atoi(stenent) icompany, _ := strconv.Atoi(scompany) go OnEvent(itenet, icompany, sclass, stype, scategory, ssession, sparam1, sparam2) } } //s := strings.Split("127.0.0.1:5432", ":") } psc.Unsubscribe("events") }
// Creates a sentinel client. Connects to the given sentinel instance, pulls the // information for the masters of the given names, and creates an intial pool of // connections for each master. The client will automatically replace the pool // for any master should sentinel decide to fail the master over. The returned // error is a *ClientError. func NewClient( network, address string, poolSize int, names ...string, ) ( *Client, error, ) { // We use this to fetch initial details about masters before we upgrade it // to a pubsub client client, err := redis.Dial(network, address) if err != nil { return nil, &ClientError{err: err} } masterPools := map[string]*pool.Pool{} for _, name := range names { r := client.Cmd("SENTINEL", "MASTER", name) l, err := r.List() if err != nil { return nil, &ClientError{err: err, SentinelErr: true} } addr := l[3] + ":" + l[5] pool, err := pool.NewPool("tcp", addr, poolSize) if err != nil { return nil, &ClientError{err: err} } masterPools[name] = pool } subClient := pubsub.NewSubClient(client) r := subClient.Subscribe("+switch-master") if r.Err != nil { return nil, &ClientError{err: r.Err, SentinelErr: true} } c := &Client{ poolSize: poolSize, masterPools: masterPools, subClient: subClient, getCh: make(chan *getReq), putCh: make(chan *putReq), closeCh: make(chan struct{}), alwaysErrCh: make(chan *ClientError), switchMasterCh: make(chan *switchMaster), } go c.subSpin() go c.spin() return c, nil }
func main() { client, err := redis.Dial("tcp", "localhost:6379") if err != nil { log.Fatal(err) } r := client.Cmd("PING") if r.Err != nil { log.Fatal(err) } log.Printf("Results from PING: %s\n", r.String()) r = client.Cmd("SET", "foo", "bar") if r.Err != nil { log.Fatal(err) } log.Printf("r.String(): %s\n", r.String()) r = client.Cmd("GET", "foo") log.Printf("r.String(): %s\n", r.String()) r = client.Cmd("SET", "foo_int", 123) r = client.Cmd("GET", "foo_int") i, _ := r.Int() log.Printf("i: %d\n", i) // Batching Commands client.Append("SET", "name", "Roberto") client.Append("GET", "name") r = client.GetReply() // Response from the SET log.Printf("Response from the SET: r.String(): %s\n", r.String()) r = client.GetReply() // Response from the GET log.Printf("Response from the GET: r.String(): %s\n", r.String()) // Use MGET client.Cmd("SET", "first_name", "Roberto") client.Cmd("SET", "last_name", "Rojas") r = client.Cmd("MGET", "first_name", "last_name") list, _ := r.List() for idx, value := range list { log.Printf("MGET - value(%d): %s\n", idx, value) } client.Close() // Pub-Sub Sample go func() { cli, _ := redis.Dial("tcp", "localhost:6379") i := 0 for { i++ cli.Cmd("PUBLISH", "news.tech", fmt.Sprintf("This is tech story #%d", i)) cli.Cmd("PUBLISH", "news.sports", fmt.Sprintf("This is sports story #%d", i)) if i > 10 { break } } }() go func() { cli, _ := redis.Dial("tcp", "localhost:6379") sub := pubsub.NewSubClient(cli) sr := sub.PSubscribe("news.*") if sr.Err != nil { log.Fatal(sr.Err) } for { r := sub.Receive() if r.Err != nil { log.Fatal(r.Err) } log.Printf("Pubsub Message: %s\n", r.Message) } }() fmt.Println("Press any key to stop program...") fmt.Scanln() // Keep the program running until any key is pressed... }
func main() { c, err := redis.DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second) errHndlr(err) defer c.Close() // select database r := c.Cmd("select", 8) errHndlr(r.Err) r = c.Cmd("flushdb") errHndlr(r.Err) r = c.Cmd("echo", "Hello world!") errHndlr(r.Err) s, err := r.Str() errHndlr(err) fmt.Println("echo:", s) //* Strings r = c.Cmd("set", "mykey0", "myval0") errHndlr(r.Err) s, err = c.Cmd("get", "mykey0").Str() errHndlr(err) fmt.Println("mykey0:", s) myhash := map[string]string{ "mykey1": "myval1", "mykey2": "myval2", "mykey3": "myval3", } // Alternatively: // c.Cmd("mset", "mykey1", "myval1", "mykey2", "myval2", "mykey3", "myval3") r = c.Cmd("mset", myhash) errHndlr(r.Err) ls, err := c.Cmd("mget", "mykey1", "mykey2", "mykey3").List() errHndlr(err) fmt.Println("mykeys values:", ls) //* List handling mylist := []string{"foo", "bar", "qux"} // Alternativaly: // c.Cmd("rpush", "mylist", "foo", "bar", "qux") r = c.Cmd("rpush", "mylist", mylist) errHndlr(r.Err) mylist, err = c.Cmd("lrange", "mylist", 0, -1).List() errHndlr(err) fmt.Println("mylist:", mylist) //* Hash handling // Alternatively: // c.Cmd("hmset", "myhash", ""mykey1", "myval1", "mykey2", "myval2", "mykey3", "myval3") r = c.Cmd("hmset", "myhash", myhash) errHndlr(r.Err) myhash, err = c.Cmd("hgetall", "myhash").Hash() errHndlr(err) fmt.Println("myhash:", myhash) //* Pipelining c.Append("set", "multikey", "multival") c.Append("get", "multikey") c.GetReply() // set r = c.GetReply() // get errHndlr(r.Err) s, err = r.Str() errHndlr(err) fmt.Println("multikey:", s) //* Publish/Subscribe // Subscribe c2, err := redis.Dial("tcp", "localhost:6379") errHndlr(err) defer c2.Close() psc := pubsub.NewSubClient(c2) psr := psc.Subscribe("queue1", "queue2") // Publish c.Cmd("publish", "queue1", "ohai") // Receive publish psr = psc.Receive() //Blocks until reply is received or timeout is tripped if !psr.Timeout() { fmt.Println("publish:", psr.Message) } else { fmt.Println("error: sub timedout") return } // Unsubscribe psc.Unsubscribe("queue1", "queue2") //Unsubscribe before issuing any other commands with c }