當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Client.Subscription方法代碼示例

本文整理匯總了Golang中github.com/fzzbt/radix/redis.Client.Subscription方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Subscription方法的具體用法?Golang Client.Subscription怎麽用?Golang Client.Subscription使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/fzzbt/radix/redis.Client的用法示例。


在下文中一共展示了Client.Subscription方法的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)
}
開發者ID:littlewhitetwo,項目名稱:radix,代碼行數:101,代碼來源:example.go


注:本文中的github.com/fzzbt/radix/redis.Client.Subscription方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。