Introduction to Redis
Redis is much more than a key-value store; it offers several data structures that can be used. This introduction will cover the basics of how to use Redis.
Installing Redis
Download and extract the code:
curl -O http://redis.googlecode.com/files/redis-2.0.0-rc1.tar.gz
tar -xzvf redis-2.0.0-rc1.tar.gz
cd redis-2.0.0-rc1
Compile and install:
make
sudo cp redis-server redis-cli redis-benchmark /usr/local/bin/
Delete the source code (optional):
cd ..
rm -rf redis-2.0.0-rc1
rm redis-2.0.0-rc1.tar.gz
Starting Redis
Use an instance provided by Redis To Go or
start a local Redis instance. For either, you need the hostname
, port
,
and password
listed below to connect to Redis.
To start a local instance:
redis-server
By default, the port is 6379
and there is no password.
Using Redis
Start the redis-cli
with the hostname
, port
, and password
redis-cli -h my-host -p 1234 -a mypassword
If you are using a local instance, the host is localhost
, the default port is 6379
, and there is no password by default.
redis-cli
GET and SET
Redis is a key-value store. By issuing the command SET foo bar
you set the value of foo
to bar
.
For example, issue the following command from the redis-cli
you just
started.
redis> set foo bar
OK
Now read the value of foo
with the GET
command.
redis> get foo
"bar"
EXPIRE and TTL
You can set keys to expire in a given amount of time by using the command, EXPIRE
. TTL
reports the time remaining before the key
expires.
redis> set foo bar
OK
redis> expire foo 120
(integer) 1
redis> ttl foo
(integer) 113
Lists
One of the distinguishing features of Redis is that the values of a key can be data-structures, rather than just values.
The following commands pertain to lists:
RPUSH
, LPUSH
, LLEN
, LRANGE
, LTRIM
, LINDEX
, LSET
,
LREM
, LPOP
, RPOP
, BLPOP
, BRPOP
, RPOPLPUSH
, and SORT
.
To create a list use LPUSH
or RPUSH
. If a list already exists, LPUSH
will add the given value to the beginning of the list and RPUSH
will add it to the end.
redis> lpush cities "San Francisco"
(integer) 1
redis> lpush cities "Boston"
(integer) 2
redis> lrange cities 0 -1
1. "San Francisco"
2. "Boston"
redis> sort cities alpha
1. "Boston"
2. "San Francisco"
redis> rpop cities
"San Francisco"
redis> lrange cities 0 -1
1. "Boston"
The SORT
command sorts the list lexicographically in ascending order with
the ALPHA
argument. To sort in descending order, append the DESC
argument to
the SORT
command.
The RPOP
command pops an element from the list's end. LPOP
pops an
element from the list's beginning.
For more information about the list command, see the Redis reference
Sets
Sets are similar to lists, except each element can occur only once. The
following commands pertain specifically to sets: SADD
, SREM
, SPOP
, SMOVE
,
SCARD
, SISMEMBER
, SINTER
, SINTERSTORE
, SUNION
, SUNIONSTORE
, SDIFF
,
SDIFFSTORE
, SMEMBERS
, SRANDMEMBER
.
redis> sadd states "Vermont"
(integer) 1
redis> smembers states
1. "Vermont"
redis> sadd states "Texas"
(integer) 1
redis> scard states
(integer) 2
redis> sadd states "Vermont"
(integer) 0
redis> smembers states
1. "Vermont"
2. "Texas"
redis> sadd states "California"
(integer) 1
redis> smembers states
1. "Vermont"
2. "Texas"
3. "California"
redis> srem states "California"
(integer) 1
redis> smembers states
1. "Vermont"
2. "Texas"
The SADD
command adds an item to the set, unless the item already exists
in the set. If the item does not exist, it is added and 1 is returned;
otherwise, 0 is returned. SMEMBERS
returns all items in the set. SCARD
returns the cardinality of the set. SREM
removes an item from the
list.
Hashs
Using a hash, you can assign values to fields in each key.The following commands
operate on sets: HSET
, HGET
, HSETNX
, HMSET
, HMGET
, HINCRBY
,
HEXISTS
, HDEL
, HLEN
, HKEYS
, HVALS
, HGETALL
.
redis> hset user name "John Doe"
(integer) 1
redis> hget user name
"John Doe"
redis> hset user address "1234 Lombard Street"
(integer) 1
redis> hget user address
"1234 Lombard Street"
redis> hgetall user
1. "name"
2. "John Doe"
3. "address"
4. "1234 Lombard Street"
In the example above, the name of user
is set to "John Doe"
using the HSET
command. The HGET
command is used to get the name
value of
user
. HGETALL
returns all the keys and values related to
the specified key.
Conclusion
For additional commands, see the Redis Command Reference.