Is there anyway to get all keys that start with a specific line in redis?

Is there anyway to get all keys that start with a specific line in redis?

I want to do something like save:

'thing1:userid1' : ' '; 'thing1:userid2' : ' '; 'thing2:userid1' : ' '; 'thing2:userid2' : ' '; 

and get every thing1 without knowing every user.

+8
javascript redis
source share
5 answers
 REDIS.keys("thing1:*") 

However, you may consider using a card.

 REDIS.hset("thing1", "user1", "") REDIS.hset("thing1", "user2", "") REDIDS.hgetall("thing1") 

Be careful with this during the manufacturing process. Redis can only process one command at a time, and KEYS can be very slow. He must look at every key in the entire database.

+10
source share

You can also track the identifiers in a list, and then retrieve the values โ€‹โ€‹of all keys (or hashes) using SORT http://redis.io/commands/SORT .

This is a bit more complicated than KEYS, but it can do more (:

I read somewhere that hgetall can give you problems if you have too many fields in the hash, unfortunately I cannot find the atm link.

+1
source share
0
source share

Redis has a KEYS command that looks for all of your key names for the specified template. However, you should be careful when using this command, since it has O (N) time complexity, so performance depends on the number of keys in your database. It is more likely to be debugged and not suitable for production environments.

0
source share

You can use the SCAN command to iterate over all the keys that match the given pattern:

 > SCAN 0 MATCH thing1:* 1) "172032" 2) 1) "thing1:userid1" 2) "thing1:userid2" > SCAN 172032 MATCH thing1:* 1) "0" 2) 1) "thing1:userid3" 
0
source share

All Articles