English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Redis supports five data types: string (string), hash (hash), list (list), set (set), and zset (sorted set: ordered set).
The string is the most basic type in Redis, which you can understand as exactly the same as Memcached, with one key corresponding to one value.
The string type is binary safe. It means that Redis's string can contain any data. For example, jpg images or serialized objects.
The string type is the most basic data type of Redis, and the maximum storage capacity of the string type value can be 512MB.
redis 127.0.0.1:6379> SET w3codebox "Basic Tutorial Website" OK redis 127.0.0.1:6379> GET w3codebox "Basic Tutorial Website"
In the above example, we used Redis's SET and GET command. The key is w3codebox, the corresponding value is Basic Tutorial Website.
Note:The maximum storage capacity of a key is 512MB.
Redis hash is a collection of key-value (key=>value) pairs.
Redis hash is a mapping table of string type field and value, which is particularly suitable for storing objects.
DEL w3codebox is used to delete the key used in the previous test, otherwise an error will occur:(error) WRONGTYPE Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379> DEL w3codebox redis 127.0.0.1:6379> HMSET w3codebox field1 "Hello" field2 "World" "OK" redis 127.0.0.1:6379> HGET w3codebox field1 "Hello" redis 127.0.0.1:6379> HGET w3codebox field2 "World"
In the example, we used Redis HMSET, HGET command,HMSET Two field=>value pairs are set, HGET gets the corresponding field corresponding to value.
Each hash can store 232 -1 Key-value pair (4More than zero billion).
Redis list is a simple string list, sorted in the order of insertion. You can add an element to the beginning (left) or the end (right) of the list.
redis 127.0.0.1:6379> DEL w3codebox redis 127.0.0.1:6379> lpush w3codebox redis (integer) 1 redis 127.0.0.1:6379> lpush w3codebox mongodb (integer) 2 redis 127.0.0.1:6379> lpush w3codebox rabbitmq (integer) 3 redis 127.0.0.1:6379> lrange w3codebox 0 10 1) "rabbitmq" 2) "mongodb" 3) "redis" redis 127.0.0.1:6379>
The list can store at most 232 - 1 elements (4294967295, each list can store4More than zero billion).
Redis's Set is an unordered collection of string types.
The set is implemented by a hash table, so the complexity of adding, deleting, and searching is O(1)。
Add a string element to the set collection corresponding to the key, return on success 1If the element is already in the set, return 0.
sadd key member
redis 127.0.0.1:6379> DEL w3codebox redis 127.0.0.1:6379> SADD w3codebox redis (integer) 1 redis 127.0.0.1:6379> SADD w3codebox mongodb (integer) 1 redis 127.0.0.1:6379> SADD w3codebox rabbitmq (integer) 1 redis 127.0.0.1:6379> SADD w3codebox rabbitmq (integer) 0 redis 127.0.0.1:6379> SMEMBERS w3codebox 1) "redis" 2) "rabbitmq" 3) "mongodb"
Note:In the above example, rabbitmq was added twice, but according to the uniqueness of the elements in the set, the second inserted element will be ignored.
The maximum number of members in the set is 232 - 1(4294967295, each set can store4(0 to 1 billion members).
Like set, zset in Redis is also a collection of string type elements and does not allow duplicate members.
The difference is that each element is associated with a double-type score. Redis sorts the members of the set from small to large through scores.
The members of zset are unique, but the scores (score) can be repeated.
Add an element to the set, if the element already exists in the set, update the corresponding score
ZADD key score member
redis 127.0.0.1:6379> DEL w3codebox redis 127.0.0.1:6379> ZADD w3codebox 0 redis (integer) 1 redis 127.0.0.1:6379> ZADD w3codebox 0 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD w3codebox 0 rabbitmq (integer) 1 redis 127.0.0.1:6379> ZADD w3codebox 0 rabbitmq (integer) 0 redis 127.0.0.1:6379> ZRANGEBYSCORE w3codebox 0 1000 1) "mongodb" 2) "rabbitmq" 3) "redis"