Databases Reference
In-Depth Information
In Redis, when you query a nonexistent list, it returns an empty list and doesn't throw an exception.
You run a range query for a nonexistent list — mylist — like so:
./redis-cli lrange mylist 0 -1
Redis returns a message: empty list or set . You can use lpush much as you use rpush to add a
member to mylist like so:
./redis-cli rpush mylist 'a member'
Now, of course mylist isn't empty and repeating a range query reveals the presence of a member .
Members can be added to a list, either on the left or on the right, and can be popped from either
direction as well. This allows you to leverage lists as queues or stacks.
For a set data structure, a member can be added using the SADD operation. Therefore, you can add
'a set member' to aset like so:
./redis-cli sadd aset 'a set member'
The command-line program would respond with an integral value of 1 confi rming that it's added to
the set. When you rerun the same SADD command, the member is not added again. You may recall
that a set, by defi nition, holds a value only once and so once present it doesn't make sense to add it
again. You will also notice that the program responds with a 0, which indicates that nothing was
added. Like sets, sorted sets store a member only once but they also have a sense of order like a list.
You can easily add 'a sset member' to a sorted set, called azset , like so:
./redis-cli zadd azset 1 'a sset member'
The value 1 is the position or score of the sorted set member. You can add another member, 'sset
member 2' , to this sorted set as follows:
./redis-cli zadd azset 4 'sset member 2'
You could verify that the values are stored by running a range operation, similar to the one you used
for a list. The sorted set range command is called zrange and you can ask for a range containing the
fi rst fi ve values as follows:
./redis-cli zrange azset 0 4
1. “a sset member”
2. “sset member 2”
What happens when you now add a value at position or score 3 and what happens when you try and
add another value to position or score 4, which already has a value?
Adding a value to azset at score 3 like so:
./redis-cli zadd azset 3 'member 3'
Search WWH ::




Custom Search