Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a range of keys with redis?

Tags:

redis

Something that comes up all the time with our data set at work is needing to query for a bunch of values given a range of keys. Date ranges are an obvious example.

I know you can use unix timestamps and a sorted set to query by date ranges, but it seems annoying, because I'd have to either

  1. put the whole document as the value in the sorted set, or
  2. just put ids in it, then ask redis for each key.

Maybe option 2 is standard? Is there a way to ask redis for multiple keys at once? Like mongodb's $in query? Or perhaps asking for a bunch of keys in a pipeline is just as fast?

like image 797
Sean Clark Hess Avatar asked Jan 16 '12 03:01

Sean Clark Hess


People also ask

How do I get Redis all keys?

Redis GET all Keys To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How do I get Redis list?

Redis Get List Items. To get elements in a Redis, use the LRANGE command. This command takes the name of the list and the index range of the element you wish to access. The command should return the values of the elements in the specified range.

What does the getRange command return?

Description. Range = getRange( AnnotObj ) returns Range , a 1-by-2 numeric array specifying the minimum and maximum positions in the reference sequence covered by annotations in AnnotObj .

Which command is used to obtain all the keys in a database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).


1 Answers

Options 2, put Ids into sorted set then use mget to get values out, if your keys are hashes then you need to issue multiple hget, but the advantage is that you can pull out specific parts of the object that you actually need instead of everything. It is very fast in practice.

like image 72
Dmytro Yashkir Avatar answered Sep 27 '22 21:09

Dmytro Yashkir