tags:

views:

41

answers:

3

Hi,

We are using Redis as a caching server, and often have to deal with caching list. When we cache simple objects we do a GET and Redis will return null if the object doesn't exist and we'll know that the object isn't cached and have to be loaded from database.

But how do we best handle the same for lists - an empty list can be a valid value. Do we need to call EXISTS to check if the list exists (but making the operation 2 calls instead of one) or does someone have a better idea how to handle this scenario?

/Thanks

A: 

If you are using php, I would assign the return value to an variable and then check if it is an array. (This is how it works using the Predis library)

$res = $redis->get('Key');
if(is_array($res))
    do code here
Colum
+1  A: 

Unfortunately, list/set retrieval commands such as LRANGE and SMEMBERS do not seem to distinguish between an empty list/set and a non-existent list/set.

So if you absolutely need to distinguish between the two cases, I guess you'll need to do an EXISTS first. Try pipelining your commands for better performance. Most Redis client libraries support pipelining.

Or you might reconsider your caching strategy so that you don't need to distinguish them.

kijin