Details of the Redis data type

  • 2020-05-24 06:25:27
  • OfStack

Details of the Redis data type

Summary:

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

String (string)

string is the most basic type of redis, which you can understand as the same type as Memcached1, with 1 key for 1 value.
The string type is base 2 safe. string means redis can contain any data. Such as jpg images or serialized objects.
The string type is the most basic data type of Redis and can store up to 512MB with one key.

The instance


redis 127.0.0.1:6379> SET name "runoob"
OK
redis 127.0.0.1:6379> GET name
"runoob"

In the above example we used the SET and GET commands of Redis. The key is name, and the corresponding value is runoob.

Note: one key can store up to 512MB.

Hash (hash)

Redis hash is a collection of key-name pairs.

Redis hash is a mapping table for field and value of type string, and hash is especially suitable for storing objects.

The instance


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

In the example above, the hash data type stores a user object that contains user script information. In the example we used the Redis HMSET, HGETALL commands, user:1 as the key values.

Each hash can store 232-1 key-value pairs (more than 4 billion).

List (list)

The Redis list is a simple list of strings, sorted by insertion order. You can add an element to either the head (left) or the tail (right) of the list.
The instance


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 rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 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)

Set of Redis is an unordered collection of type string.

The collection is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1).

sadd command

Add an string element to the set collection corresponding to key and return 1 successfully. If the element has already returned 0 in the collection,key corresponding to set does not return error.

sadd key member

The instance


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 rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers runoob

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

Note: rabitmq was added twice in the above example, but the element inserted the second time will be ignored based on the singleness of the elements in the collection.

The largest number of members in a collection is 232-1 (4294967295, each containing more than 4 billion members).

zset(sorted set: ordered collection)

Redis zset and set 1 are also collections of string type elements, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double. redis USES fractions to rank the members of a collection from smallest to largest.

Members of zset are one-only, but scores (score) can be repeated.

zadd command

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

zadd key score member

The instance


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 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE runoob 0 1000

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

The above is the detailed explanation of Redis data type. If you have any questions, please leave a message or come to the community of this website for discussion. Thanks for reading.


Related articles: