Redis data type
Redis supports five data types: string, hash, list, set and Zset.
String (string)
As like as two peas, string is the most basic type of redis. You can understand it as a type that is exactly the same as Memcached, and a key corresponds to a value.
The string type is binary safe. This means that the redis string can contain any data. For example, jpg images or serialized objects.
String type is the most basic data type of Redis. The value of string type can store 512MB at most.
example
redis 127.0.0.1:6379> SET runoob "ha-ha" OK redis 127.0.0.1:6379> GET runoob "ha-ha"
In the above example, we used the SET and GET commands of Redis. The key is runoob and the corresponding value is ha ha.
Note: one key can store 512MB at most.
Hash (hash)
Redis hash is a collection of key value (key = > value) pairs.
Redis hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects.
example
DEL runoob is used to delete the key used in the previous test, otherwise an error will be reported: (error) WRONGTYPE Operation against a key holding the wrong kind of value
redis 127.0.0.1:6379> DEL runoob redis 127.0.0.1:6379> HMSET runoob field1 "Hello" field2 "World" "OK" redis 127.0.0.1:6379> HGET runoob field1 "Hello" redis 127.0.0.1:6379> HGET runoob field2 "World"
In the example, we use redis HMSET and HGET commands. HMSET sets two field = > value pairs, and HGET obtains the value corresponding to the corresponding field.
Each hash can store 2 32 - 1 key value pairs (more than 4 billion).List
Redis list is a simple string list, 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> DEL runoob redis 127.0.0.1:6379> lpush runoob redis (integer) 1 redis 127.0.0.1:6379> lpush runoob mongodb (integer) 2 redis 127.0.0.1:6379> lpush runoob rabbitmq (integer) 3 redis 127.0.0.1:6379> lrange runoob 0 10 1) "rabbitmq" 2) "mongodb" 3) "redis" redis 127.0.0.1:6379>
The list can store up to 232 - 1 elements (4294967295, each list can store more than 4 billion).
Set
Redis Set is an unordered Set of string type.
The set is implemented through hash table, so the complexity of adding, deleting and searching is O(1).
sadd command
Adding a string element to the set set corresponding to the key returns 1 successfully. If the element has already returned 0 in the set.
sadd key member
example
redis 127.0.0.1:6379> DEL runoob redis 127.0.0.1:6379> sadd runoob redis (integer) 1 redis 127.0.0.1:6379> sadd runoob mongodb (integer) 1 redis 127.0.0.1:6379> sadd runoob rabbitmq (integer) 1 redis 127.0.0.1:6379> sadd runoob rabbitmq (integer) 0 redis 127.0.0.1:6379> smembers runoob
1) "redis"
2) "rabbitmq"
3) "mongodb"
Note: rabbitmq has been added twice in the above example, but according to the uniqueness of the elements in the collection, the elements inserted the second time will be ignored.
The largest number of members in the collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).
zset(sorted set)
Redis zset, like set, is also a collection of string elements, and duplicate members are not allowed.The difference is that each element is associated with a score of type double. redis sorts the members of the collection from small to large through scores.
The members of zset are unique, but the score can be repeated.
zadd command
Add an element to the collection, and update the corresponding score if the element exists in the collection
zadd key score member
example
redis 127.0.0.1:6379> DEL runoob redis 127.0.0.1:6379> zadd runoob 0 redis (integer) 1 redis 127.0.0.1:6379> zadd runoob 0 mongodb (integer) 1 redis 127.0.0.1:6379> zadd runoob 0 rabbitmq (integer) 1 redis 127.0.0.1:6379> zadd runoob 0 rabbitmq (integer) 0 redis 127.0.0.1:6379> ZRANGEBYSCORE runoob 0 1000 1) "mongodb" 2) "rabbitmq" 3) "redis"