sprabhakaran
sprabhakaran

Reputation: 1635

How to check Equals on two lists in Redis

I have two lists in Redis. How can I check those lists are equal? Is the only option is to get elements from the list one-by-one and compare to the list in memory?

Is Redis provide any workarounds?

Upvotes: 3

Views: 585

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49942

There are no workarounds, Redis has no digest function for Lists (or other data types for that matter) and since these are doubly-linked there's no other way to compare them other than element by element.

You could make the comparison in Lua however, to make it more efficient compared to performing it client-side. Something like the following should work:

if redis.call('LLEN',KEYS[1]) == redis.call('LLEN',KEYS[2]) and
    redis.call('LRANGE',KEYS[1],0,0)[1] == redis.call('LRANGE',KEYS[2],0,0)[1] and
    redis.call('LRANGE',KEYS[1],-1,-1)[1] == redis.call('LRANGE',KEYS[2],-1,-1)[1] and
    redis.call('DUMP',KEYS[1]) == redis.call('DUMP',KEYS[2]) then
    return 'Lists are the same'
end
return 'Lists are not the same'

The script above resorts to full comparison only if the lists are of different sizes and their ends (which are cheap to fetch) are identical.

Upvotes: 7

Related Questions