Golang. Bulk acquisition of cached values in Redis

Source: Internet
Author: User

21-day Boutique blockchain free learning, in-depth expert led the way, to help developers easily play the blockchain! >>>

Using Redis's pipeline, when retrieving the key value in Redis in bulk, the error, analysis found that when the batch acquisition, exec, will return two values: nil and Redis. Nil,

In this case, the use of conventional if Err!=nil judgment method to determine errors, will be error.

Test code:

package mainimport ("github.com/go-redis/redis""log""strconv")func main() {client := redis.NewClient(&redis.Options{Addr:     "localhost:6379",Password: "", // no password setDB:       0,  // use default DBNetwork:  "tcp",PoolSize: 50,})if _, err := client.Ping().Result(); err != nil {panic(err)}pipe := client.Pipeline()log.Println("所有的key都存在:")for i := 0; i < 4; i++ {key := "n:" + strconv.FormatInt(int64(i), 10)pipe.Get(key)}result, err := pipe.Exec()log.Println(result)log.Println(err == nil)log.Println(err == redis.Nil)log.Println("有不存在的key:")for i := 0; i < 5; i++ {key := "n:" + strconv.FormatInt(int64(i), 10)pipe.Get(key)}result, err = pipe.Exec()log.Println(result)log.Println(err == nil)log.Println(err == redis.Nil)defer client.Close()}

Operation Result:

As you can see, if there is a nonexistent key in the bulk fetch key, the return will be redis. Nil, otherwise the return is nil.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.