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


Golang Client.Mset方法代碼示例

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


在下文中一共展示了Client.Mset方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {
	var c *redis.Client

	conf := redis.DefaultConfig()
	// Network and address
	//
	// Default are "tcp" and "127.0.0.1:6379" so these are commented out.

	// TCP example:
	// conf.Network = "tcp"
	// conf.Address = "127.0.0.1:6379"

	// Unix example:
	// conf.Network = "unix"
	// conf.Address = "/tmp/redis.sock"

	conf.Database = 8                              // Database number
	conf.Timeout = time.Duration(10) * time.Second // Socket timeout
	c = redis.NewClient(conf)

	defer c.Close()

	//** Blocking calls
	rep := c.Flushdb()
	if rep.Err != nil {
		fmt.Println("redis:", rep.Err)
		return
	}

	//* Strings

	// It's generally good idea to check for errors like this,
	// but for the sake of keeping this example short we'll omit these from now on.
	if rep = c.Set("mykey0", "myval0"); rep.Err != nil {
		fmt.Println("redis:", rep.Err)
		return
	}

	var err error
	var s string
	if rep = c.Get("mykey0_may_not_exist"); rep.Type == redis.ReplyNil {
		//key not found
		fmt.Println("key not found ")

	} else if s, err = rep.Str(); err != nil {

		fmt.Println("Get error ", err)

		return
	}
	fmt.Println("mykey0:", s)

	myhash := map[string]string{
		"mykey1": "myval1",
		"mykey2": "myval2",
		"mykey3": "myval3",
	}

	// Alternatively:
	// c.Mset("mykey1", "myval1", "mykey2", "myval2", "mykey3", "myval3")
	c.Mset(myhash)

	ls, err := c.Mget("mykey1", "mykey2", "mykey3").List()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("mykeys values:", ls)

	//* List handling
	mylist := []string{"foo", "bar", "qux"}

	// Alternativaly:
	// c.Rpush("mylist", "foo", "bar", "qux")
	c.Rpush("mylist", mylist)

	mylist, err = c.Lrange("mylist_may_not_exist", 0, -1).List()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("mylist:", mylist)

	//* Hash handling

	// Alternatively:
	// c.Hmset("myhash", ""mykey1", "myval1", "mykey2", "myval2", "mykey3", "myval3")
	c.Hmset("myhash", myhash)

	myhash, err = c.Hgetall("myhash").Hash()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("myhash:", myhash)

	//* Multicalls
//.........這裏部分代碼省略.........
開發者ID:littlewhitetwo,項目名稱:radix,代碼行數:101,代碼來源:example.go


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