示例#1
0
func asyncPublish(spec *redis.ConnectionSpec, channel string) {

	client, e := redis.NewAsynchClientWithSpec(spec)
	if e != nil {
		log.Println("failed to create the async client", e)
		return
	}

	// ref will ultimately point to the last future returned from Publish()
	var rcvCntFuture redis.FutureInt64
	for i := 0; i < 100; i++ {
		msg := []byte(fmt.Sprintf("this is message # %d (using async client)!", i))
		var err redis.Error
		// publish the message and don't wait for the future
		// we only care if an error was raised on publish
		rcvCntFuture, err = client.Publish(channel, msg)
		if err != nil {
			fmt.Printf("Error on Publish - %s", err)
		}
	}

	// ok, now let's wait until the last publish's future is done
	// before quiting.
	rcvCnt, fe := rcvCntFuture.Get()
	if fe != nil {
		fmt.Printf("Error on future get - %s\n", fe)
		return
	} else {
		fmt.Printf("(LAST) Message sent to %d subscribers\n", rcvCnt)
	}

	client.Quit()
}
示例#2
0
func doDecr(id string, signal chan int, client redis.AsyncClient, cnt int) {
	var fr redis.FutureInt64
	key := "ctr-" + id
	for i := 0; i < cnt; i++ {
		fr, _ = client.Decr(key)
	}
	fr.Get()
	signal <- 1
}
示例#3
0
func doIncr(id string, signal chan int, client redis.AsyncClient, cnt int) {
	key := "ctr-" + id
	var fr redis.FutureInt64
	for i := 0; i < cnt; i++ {
		fr, _ = client.Incr(key)
	}
	v, _ := fr.Get()
	if v != int64(cnt) {
		log.Fatalf("BUG: expecting counter %s to be %d but it is %d\n", key, cnt, v)
		panic(1)
	}
	// debug sanity check
	//	log.Printf("worker[%s] - last INCR result %s=%d\n", id, key, v)
	signal <- 1
}