I am new to Redis, so excuse the question.
I am trying to make a list of hashes. For example (in JSON):
{
userList: [
{ id: 1, name: \'Foo\', rank
How do you implement the last schema in Redis? Is it possible?
Redis is schema-less. Let's call what you need a data storage approach.
One possible approach is using HSET
or HMSET to add these JSON objects by id, where their id is the key and the JSON text is the value. We'll call this hash as users:byid
.
This is the first part of the problem. Now you can get objects by id.
Now the next issue is you want to retrieve objects in a range of what you call ranking. In order to get this, you need to store your objects in a sorted set using ZADD
. Sorted sets are sorted by score, and items are stored with a score. It sounds perfect for your use case!
Actually you're going to store the object ids in the whole sorted set:
zadd users:byranking 10 1 5 2
... where 10
is the score (i.e. your actual ranking value) and 1 the id and so on.
So, how you filter items by ranking? Using ZRANGEBYSCORE:
zrangebyscore users:byranking 0 (10
zrangebyscore users:byranking 0 10
The so-called ZRANGEBYSCORE
will give you the ids of retrieved users. How you get their JSON text? Using HMGET:
HMGET users:byid 1 2
...which will get both user with id 1
and 2
, if 10
ranking is inclusive.