Redis command o...login
Redis command operation Chinese manual
author:php.cn  update time:2022-04-12 14:07:28

Redis data type


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


String (string)

String is the most basic type of redis. You can understand it as exactly the same type as Memcached. One key corresponds to one value.

The string type is binary safe. This means that the redis string can contain any data. For example, jpg pictures or serialized objects .

The string type is the most basic data type of Redis, and a key can store up to 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 the SET and GET commands of Redis. The key is name, and the corresponding value is w3cschool.cc.

Note: A key can store up to 512MB.


Hash (Hash)

Redis hash is a collection of key-value pairs.

Redis hash is a mapping table of string type fields and values. Hash is particularly 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>

In the above example, the hash data type stores the user object containing user script information. In the example, we used 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).



List (List)

Redis lists are simple lists of strings, sorted in 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 232 - 1 elements (4294967295, each list can store more than 4 billion).


Set (set)

Redis’ Set is an unordered collection of string type.

Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1).

sadd command

Adds a string element to the set collection corresponding to the key, and returns 1 successfully. If the element is in the collection and returns 0, and the set corresponding to the key does not exist, an error will be returned.

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 added 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).


zset (sorted set: ordered set)

Redis zset, like set, is also a collection of string type elements, and duplicate members are not allowed.


The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large.

The members of zset are unique, but the scores can be repeated.

zadd command

Add elements to the collection, and update the corresponding score if the element exists in the collection

zadd key score member

Instance

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"

php.cn