Latest web development tutorials

Redis data types

Redis supports five data types: string (string), hash (hash), list (list), set (set) and zset (sorted set: ordered set).


String (String)

redis string is the most basic type, you can understand exactly the same type as with Memcached, a key corresponding to a value.

string type is binary safe. Meaning redis the string can contain any data. Such as jpg image or a sequence of objects.

Redis string type is the basic data types, a key can be stored maximum 512MB.

Examples

redis 127.0.0.1:6379> SET name "w3big"
OK
redis 127.0.0.1:6379> GET name
"w3big"

In the example above we use Redis ofSET and GETcommands. Key is name, the corresponding value w3big.

Note: A key can store maximum 512MB.


Hash (hash)

Redis hash is a collection of key-value pairs.

Redis hash is a string type of field and value mapping table, hash is particularly suitable for storing objects.

Examples

127.0.0.1:6379> HMSET user:1 username w3big password w3big points 200
OK
127.0.0.1:6379> HGETALL user:1
1) "username"
2) "w3big"
3) "password"
4) "w3big"
5) "points"
6) "200"

Examples of the above data types stored in the hash of the user object that contains user scripts. Examples we use Redis HMSET, HGETALLcommand, user: 1 as key.

Each hash can store key-value pairs 232-1 (4000000000).


List (list)

Redis list is a simple list of strings sorted insertion order. You can add an element to the head of the list (left) or rear (on the right).

Examples

redis 127.0.0.1:6379> lpush w3big redis
(integer) 1
redis 127.0.0.1:6379> lpush w3big mongodb
(integer) 2
redis 127.0.0.1:6379> lpush w3big rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange w3big 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>

List can store up 232--1 element (4,294,967,295 each list can store more than 4 billion).


Set (collection)

Redis is an unordered collection of Set string type.

Set by the hash table to achieve, so add, delete, find the complexity is O (1).

sadd command

Adding to a string element, key corresponding set collection, successful return 1 return 0 if the element already in the collection, key corresponding to the set does not exist an error is returned.

sadd key member

Examples

redis 127.0.0.1:6379> sadd w3big redis
(integer) 1
redis 127.0.0.1:6379> sadd w3big mongodb
(integer) 1
redis 127.0.0.1:6379> sadd w3big rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd w3big rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers w3big

1) "rabitmq"
2) "mongodb"
3) "redis"

Note: The above example rabitmq added twice, but based on a unique set of elements within the second insertion element is ignored.

Set the maximum number of members 232--1 (4294967295 Each set can store more than 40 million members).


zset (sorted set: an ordered collection)

Redis zset and is set as a collection of elements of type string, and does not allow duplicate members.

The difference is that are associated with each type of element of a double score. It is through redis scores for small to large order of collection members.

Zset member is unique, but the score (score) it can be repeated.

zadd command

Add an element to the collection, the element is present in the collection updates the corresponding score

zadd key score member 

Examples

redis 127.0.0.1:6379> zadd w3big 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd w3big 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd w3big 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd w3big 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE w3big 0 1000

1) "redis"
2) "mongodb"
3) "rabitmq"