Remember once Golang read the Redis cache, the process of counting the daily life

Source: Internet
Author: User

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

One recent requirement is to read the daily live from the Redis cache, where the daily live and number of each game is kept in the Redis cache, with the type Hashset,key is $game_id: $log _date,value is the player ID and the corresponding number of innings, the structure is as follows:

The number of game_id=1 games in the 2018-07-18 is as follows:

user_id Number of offices
1 2
2 3
3 4
4 5

Although you can know all the game_id, but some games may not live, a game_id to get the daily work is not very efficient, so you can execute the redis keys command to get all the keys of the day Live:

Then traverse each key, call the Hgetall command to get each key corresponding to the daily live and the number of information, you can draw the daily live and the number of each game, corresponding to the Golang code as follows:

import ("testing""github.com/garyburd/redigo/redis")func TestRedis(t *testing.T){// 连接rediscon,err := redis.Dial("tcp", "127.0.0.1:6379")if err != nil {t.Errorf("连接redis出错:err:%v\n", err)}defer con.Close()// 查询所有的keyscacheName := "*:20180718"keys,err := redis.Strings(con.Do("keys", cacheName))for _,key := range keys {activeInfo,err := redis.IntMap(con.Do("hgetall", key))if err != nil {t.Errorf("查询单个key的日活信息出错:err:%v\n", err)}t.Logf("key-%v, activeInfo-%+v\n", key, activeInfo)}}

The corresponding output is as follows:

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.