Getting last set / hash from redis

I am just starting out with redis, and I hit my first stumbling block with noSql output; previously all i know is sql server.

I understand the principle that everything is based on core value. but how does this work with ordering, for example:

using hash set:

HMSET users:1 firstname 'james' lastname 'smith' created 'datetime.datatime.now' 

Now I assume that you add the second entry, you get the length of the hash set (in this case, we say that its 1 returned the value "x"), then we add another line

 HMSET users:x firstname 'john' lastname 'smith' created 'datetime.datatime.now' 

How do you get the latest entry? by date? or can you just say "get a record of -1 from hashset"?

Perhaps I suggest using a hashset when sorted sets are more suitable?

I use python if it matters

+4
source share
2 answers

You will need to check the SORT command.

You can sort by creating a timestamp if it is stored in an era.

 > HMSET users:1 firstname 'john' lastname 'smith' created 1319729878 "OK" > HMSET users:2 firstname 'Jane' lastname 'Forbes' created 1319729910 "OK" > sadd users 1 true > sadd users 2 true > sort users get users:*->firstname by users:*->created ["john","Jane"] > sort users get users:*->firstname by users:*->created desc ["Jane","john"] 

You can get several keys, if you want, SORT is a likely command with most options, study the document.

About keys, you should carefully think about the possible reuse of the key (delete, count + 1, insert will reuse the key?), So I just get the keys from the relational database in my project.

+2
source

You should use a sorted set of user IDs, when you add to the set, add the user ID and timestamp as a metric.

Then you can pull them in ascending or desc order using something like zrevrange, limit this to 1 entry to get the latest version.

Then you can get all the values ​​from your hash.

0
source

All Articles