Redis Data types
Redis supports five types of data: string (String), hash (hash), list, set (set), and Zset (sorted set: Ordered set).
String (String)
The string is the most basic type of redis, and you can understand it as a type that is identical to memcached, a key that corresponds to a value.
The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.
The string type is the most basic data type of Redis, and a key can store up to 512MB.
Instance
- Redis 127.0. 0.1:6379> SET name "Shouce.ren"
- Ok
- Redis 127.0. 0.1:6379> GET name
- "Shouce.ren"
In the above example we used Redis's SET and GET commands. The key is name and the corresponding value is Shouce.ren.
Note: a key can store up to 512MB.
Hash (hashed)
A Redis hash is a collection of key-value pairs.
Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.
Instance
- Redis 127.0. 0.1:6379> hmset user:1 username shouce. ren password shouce. ren points
- Ok
- Redis 127.0. 0.1:6379> hgetall user:1
- 1) "username"
- 2) "Shouce.ren"
- 3) "password"
- 4) "Shouce.ren"
- 5) "points"
- 6) "$"
- Redis 127.0. 0.1:6379>
The hash data type in the above example stores the user object that contains the user script information. In the example we used Redis hmset, hegtall command,user:1 as the key value.
Each hash can store 232-1 key-value pairs (more than 4 billion).
List (lists)
The Redis list is a simple list of strings, sorted by insertion order. You can add an element guide to the head (left) or tail (right) of the list.
Instance
- Redis 127.0. 0.1:6379> lpush shouce. ren redis
- (integer) 1
- Redis 127.0. 0.1:6379> lpush shouce. ren mongodb
- (integer) 2
- Redis 127.0. 0.1:6379> lpush shouce. ren rabitmq
- (integer) 3
- Redis 127.0. 0.1:6379> lrange shouce. ren 0
- 1) "RABITMQ"
- 2) "MongoDB"
- 3) "Redis"
- Redis 127.0. 0.1:6379>
The list can store up to 232-1 elements (4294967295, each of which can store more than 4 billion).
Set (SET)
Redis's set is an unordered collection of type string.
The collection is implemented by a hash table, so the complexity of adding, deleting, and finding is O (1).
Sadd command
Adds a string element to the set set of the key corresponding to the successful return 1, if the element and the returned 0,key in the collection, the corresponding set does not have a return error.
- Sadd Key Member
Instance
- Redis 127.0. 0.1:6379> sadd shouce. ren redis
- (integer) 1
- Redis 127.0. 0.1:6379> sadd shouce. ren mongodb
- (integer) 1
- Redis 127.0. 0.1:6379> sadd shouce. ren rabitmq
- (integer) 1
- Redis 127.0. 0.1:6379> sadd shouce. ren rabitmq
- (integer) 0
- Redis 127.0. 0.1:6379> smembers shouce. ren
- 1) "RABITMQ"
- 2) "MongoDB"
- 3) "Redis"
Note: RABITMQ has been added two times in the above example, but the second inserted element is ignored based on the uniqueness of the elements within the collection.
The maximum number of members in the collection is 232-1 (4294967295, each of which can store 40多亿个 members).
Zset (sorted set: Ordered set)
Redis Zset and set are also collections of string-type elements and do not allow duplicate members.
The difference is that each element is associated with a double-type fraction. Redis is a small-to-large ordering of the members in a collection by fractions.
Zset members are unique, but fractions (score) can be duplicated.
Zadd command
Adds an element to the collection, the element exists in the collection, and updates the corresponding score
- Zadd Key Score Member
Instance
- Redis 127.0. 0.1:6379> zadd shouce. ren 0 redis
- (integer) 1
- Redis 127.0. 0.1:6379> zadd shouce. ren 0 mongodb
- (integer) 1
- Redis 127.0. 0.1:6379> zadd shouce. ren 0 rabitmq
- (integer) 1
- Redis 127.0. 0.1:6379> zadd shouce. ren 0 rabitmq
- (integer) 0
- Redis 127.0. 0.1:6379> zrangebyscore shouce. ren 0
- 1) "Redis"
- 2) "MongoDB"
- 3) "RABITMQ"
Introduction to Redis data types