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: