Learning Redis is not that difficult

For those programmers who do not like SQL, Redis is a good choice for your applications.Redis is a Key Value storage engine or data structure engine.Redis supports five different data structures: strings, hashes, lists, sets and ordered sets. Each one has unique characteristics and supports unique commands. Regardless of the type, a value is accessed by a key.


You can store an incrementing integer counter, an actual string, or a binary serialized object (blob). The most common commands are SET and GET:

redis> SET pages:home "about us"
redis> GET pages:home
"about us"


Rather than manipulating a key directly (like with a String) you manipulate the fields of a key. We don’t just get and set a hash value. Instead, we get and set the value of a field for a hash.
The most common commands are HSET and HGET:

redis> HSET key field value
redis> HGET key field

What is the different between hash and string? For example:

redis> SET users:1000 {name: 'gary', password: 1234}
redis> HSET users:1000 name gary
redis> HSET users:1000 password 1234

Before making your decision, you should ask yourself a question: how you’re going to manipulate and query it. If you need control over individual fields, and you don’t want to pull the whole object into your application, use a hash. Otherwise, a String is probably what you want.


Lists let you associate an array of values to a single key. In fact, you can think of them as dynamic arrays and use insert, append, pop, push, and trim method.

redis> RPUSH mylist value1 (append value to the list)
redis> RPUSH mylist value2
redis> RPUSH mylist value3
redis> LRANGE mylist 0 -1 
redis> LPUSH mylist value4 (prepend value to a list)
redis> LPUSH mylist value5
redis> LRANGE mylist 0 -1 

redis> LLEN mylist (Get the length of a list)
(integer) 5

redis>LINDEX mylist 0 (Returns the element at index 0 in the list stored at key)

redis> RPOP mylist (Removes and returns the last element of the list stored at key)


Sets are a lot like lists, except they provide set-semantics (no duplicate values in a given set).

redis>SADD myset "hi"
redis>SADD myset "how"
redis>SADD myset "hi" (duplicated value will be ignored)
redis>SMEMBERS myset (Get all the members in a set)
1) "hi"
2) "how"

redis>SCARD myset (get the number of elements of the set)
(integer) 2

Sorted Sets:

A sorted set is similar to a set, except each value is associated and sorted by a score field. In other words, when you add a value to a sorted set, you also specify the score as a number. This determines the order of the value within the set.

redis> ZADD myzset 1 "one"  (Add one or more members to a sorted set, or update its score if it already exists)
redis> ZADD myzset 2 "two"
redis> ZADD myzset 3 "three"
redis> ZADD myzset 4 "one" (update the score)
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "two"
2) "2"
3) "three"
4) "3"
reids>  ZCARD myzset (get the number of members in set)
(integer) 3

Redis key commands:

DEL key (delete a key)
EXISTS key (Determine if a key exists)
KEYS pattern (Returns all keys matching pattern)

This entry was posted in open-source. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s