Home  >  Article  >  Backend Development  >  Redis obtains hash key data in batches

Redis obtains hash key data in batches

ringa_lee
ringa_leeOriginal
2018-05-14 16:55:006646browse

Since user information is cached in the redis hash type

The requirement is to obtain a user list, such as 30 users on each page, and want to obtain the user information of these 30 users at one time
But redis does not obtain hash keys in batches Methods!
How should I solve it? Is there something wrong with my design?
For example, the user hash key is userinfo:1 user:info:2 user:info:3 user:info:4 ....
These hash keys store user information
I want to get the hash value of userinfo:1-30 at one time

Reply content:

Since user information is cached in the redis hash type
The requirement is to obtain a user list, such as 30 users per page, and want to obtain the user information of these 30 users at once
But redis does not batch How to get hash key!
How should I solve it? Is there something wrong with my design?
For example, the user hash key is userinfo:1 user:info:2 user:info:3 user:info:4 ....
These hash keys store user information
I want to get the hash value of userinfo:1-30 at one time

Use lua script to loop
eval "local rst={}; for i,v in pairs(KEYS) do rst[i]=redis.call('hgetall', v) end; return rst" 2 user:1 user :2

127.0.0.1:6379> hgetall user:2
1) "age"
2) "22"
3) "name"
4) "tom"
127.0.0.1:6379> hgetall user:1
1) "name"
2) "jack"
3) "age"
4) "21"
127.0.0.1:6379> eval "local rst={}; for i,v in pairs(KEYS) do rst[i]=redis.call('hgetall', v) end; return rst" 2 user:1 user:2
1) 1) "name"
   2) "jack"
   3) "age"
   4) "21"
2) 1) "age"
   2) "22"
   3) "name"
   4) "tom"

Use lua, or multi

In my case it’s just a for loop, multi is a transaction

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn