Pranav
Pranav

Reputation: 2172

option for lexicographical order in zrange?

When i add a score for a key using zincrby, it increases the score and puts the element in lexicographical order.

Can i get this list in the order, in which the elements are updated or added ?

e.g>

If I execute

zincrby A 100 g
zincrby A 100 a
zincrby A 100 z

and then

zrange A 0 -1

then the result is

a->g->z

where, i want the result in order the entries are made so,

g->a->z

As score is same for all, redis is placing the elements in lexicographical order. Is there any way to prevent it ?

Upvotes: 0

Views: 481

Answers (1)

zenbeni
zenbeni

Reputation: 7193

I don't think it is possible, but if you want to keep the order of insertion with scores, you should manipulate something like this:

<score><timestamp>

instead of

<score>

You will have to define a good time record (millis should be ok). Then you can use

zincrby A 100 * (10^nbdigitsformillis)

For instance:

Score = 100 and timestamps is 1381377600 seconds

That gives: 1001381377600

You incr by 200 the score: 1001381377600 + 200 * 10 = 3001381377600

Be careful with zset as it stores scores with double values (64 bits, but only 52 available for int value) so don't store more than 15-17 digits.

If you can't do that (need for great timestamp precision, and great score precision), you will have to manage two zsets (one for actual score, one for timestamp) and managing your ranking manual with the two values.

Upvotes: 1

Related Questions