Redis data types (4)

1. Redis data type

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

String (string)

String is the most basic type of redis, you can understand it as the exact same type as Memcached, a key corresponds to a value.
The string type is binary safe. Means redis string can contain any data. Such as jpg images or serialized objects.
The string type is the most basic data type of Redis, and a key can store a maximum of 512MB.

 Example

redis 127.0.0.1:6379> SET name "w3cschool.cc"
OK
redis 127.0.0.1:6379> GET name
"w3cschool.cc"

 In the above example we used Redis's SET and GET commands. The key is name, and the corresponding value is w3cschool.cc.
Note: A key can store up to 512MB.

2. HASH

A Redis hash is a collection of key-value pairs.
Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.

 Example

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

 The hash data type in the above example stores the user object containing the user script information. In the example, we use the Redis HMSET, HGETALL command, and user:1 is the key value.
Each hash can store 232 - 1 key-value pairs (more than 4 billion).

3. List (list)

Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.

Example

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

 Lists can store up to 2 32 - 1  elements (4294967295, each list can store more than 4 billion).

4. Set (collection)

 

Redis's Set is an unordered collection of string type.

The set is implemented by a hash table, so the complexity of adding, deleting, and searching is O(1)

sadd command

Add a string element to the set set corresponding to the key, and return 1 successfully. If the element and the set return 0 in the set, the set corresponding to the key does not exist and returns an error.

 

sadd key member

 Example

redis 127.0.0.1:6379> sadd w3cschool.cc redis
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cc mongodb
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cc rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cc rabitmq
(integer) 0
redis 127.0.0.1:6379>smembers w3cschool.cc

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

 

Note: Rabitmq adds twice in the above example, but according to the uniqueness of the elements in the collection, the second inserted element will be ignored.
The maximum number of members in a collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).

Five, zset (sorted set: ordered set)

 Redis zset, like set, is also a collection of elements of type string, and does not allow duplicate members.
The difference is that each element is associated with a fraction of type double. Redis sorts the members of the set from small to large through scores.
The members of zset are unique, but the score can be repeated.

The zadd command
adds elements to the collection, and if the element exists in the collection, the corresponding score is updated

zadd key score member

 Example

redis 127.0.0.1:6379 > zadd w3cschool.cc 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd w3cschool.cc 0 mongodb
(integer) 1
redis 127.0.0.1:6379 > zadd w3cschool.cc 0 rabitmq
(integer) 1
redis 127.0.0.1:6379 > zadd w3cschool.cc 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE w3cschool.cc 0 1000

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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326912859&siteId=291194637