本文整理匯總了Golang中github.com/fzzbt/radix/redis.Client.Publish方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Publish方法的具體用法?Golang Client.Publish怎麽用?Golang Client.Publish使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/fzzbt/radix/redis.Client
的用法示例。
在下文中一共展示了Client.Publish方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
//.........這裏部分代碼省略.........
rep = c.Transaction(func(mc *redis.MultiCall) {
mc.Set("trankey", "tranval")
mc.Get("trankey")
})
if rep.Err != nil {
fmt.Println(rep.Err)
return
}
s, err = rep.Elems[1].Str()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("trankey:", s)
//* Complex transactions
// Atomic INCR replacement with transactions
myIncr := func(key string) *redis.Reply {
return c.MultiCall(func(mc *redis.MultiCall) {
var curval int
mc.Watch(key)
mc.Get(key)
rep := mc.Flush()
if rep.Err != nil {
return
}
s, err := rep.Elems[1].Str()
if err == nil {
curval, err = strconv.Atoi(s)
}
nextval := curval + 1
mc.Multi()
mc.Set(key, nextval)
mc.Exec()
})
}
myIncr("ctrankey")
myIncr("ctrankey")
myIncr("ctrankey")
s, err = c.Get("ctrankey").Str()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ctrankey:", s)
//** Asynchronous calls
c.Set("asynckey", "asyncval")
fut := c.AsyncGet("asynckey")
// do something here
// block until reply is available
s, err = fut.Reply().Str()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("asynckey:", s)
//* Pub/sub
msgHdlr := func(msg *redis.Message) {
switch msg.Type {
case redis.MessageMessage:
fmt.Printf("Received message \"%s\" from channel \"%s\".\n", msg.Payload, msg.Channel)
case redis.MessagePmessage:
fmt.Printf("Received pattern message \"%s\" from channel \"%s\" with pattern "+
"\"%s\".\n", msg.Payload, msg.Channel, msg.Pattern)
default:
fmt.Println("Received other message:", msg)
}
}
sub, err_ := c.Subscription(msgHdlr)
if err_ != nil {
fmt.Printf("Failed to subscribe: '%s'!\n", err_)
return
}
defer sub.Close()
sub.Subscribe("chan1", "chan2")
sub.Psubscribe("chan*")
c.Publish("chan1", "foo")
sub.Unsubscribe("chan1")
c.Publish("chan2", "bar")
// give some time for the message handler to receive the messages
time.Sleep(time.Second)
}