Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Redis data type


May 16, 2021 Redis


Table of contents


Redis data type

Redis supports five data string(字符串) hash(哈希) list(列表) set(集合) and zset(sorted set:有序集合)


String (string)

string is the most basic type of redis which you can understand as exactly the same type as Memcached key corresponding to one value

string type is binary secure. T he redis string contain any data. For jpg pictures or serialized objects.

string type is the most basic data type in Redis with a key that can store up to 512MB.

Instance

redis 127.0.0.1:6379> SET name "w3cschool.cn"
OK
redis 127.0.0.1:6379> GET name
"w3cschool.cn"

In the above example, we used Redis SET and GET The key name and the corresponding value is w3cschool.cn.

Note: A key can store up to 512MB.


Hash (Hash)

Redis hash is a (key=>value)

Redis hash a string field and value and hash is ideal for storing objects.

Instance

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

In the above hash data type stores user objects that contain user script information. I n the example we used Redis HMSET, HGETALL command, user:1 the key value. Each hash can store 2 32 - 1 key pairs (more than 4 billion).


List (list)

Redis list is a simple list of strings, sorted in the order in which they were inserted. You can add an element to the head (left) or tail (right) of the list.

Instance

redis 127.0.0.1:6379> lpush w3cschool.cn redis
(integer) 1
redis 127.0.0.1:6379> lpush w3cschool.cn mongodb
(integer) 2
redis 127.0.0.1:6379> lpush w3cschool.cn rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange w3cschool.cn 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>

Lists can store up to 2 32 - 1 elements (4294967295, more than 4 billion per list).


Set (Collection)

Redis Set a string collection of string types.

Collections are implemented through hash tables, so adding, deleting, and finding are all O(1).

Sadd command

Adding a string set collection for key set 1, if the element has already returned 0 in the key set not have a return error.

sadd key member

Instance

redis 127.0.0.1:6379> sadd w3cschool.cn redis
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cn mongodb
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cn rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd w3cschool.cn rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers w3cschool.cn

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

Note: rabitmq the above instance, but the second inserted element is ignored depending on the uniqueness of the elements within the collection.

The largest number of members in a collection is 2 32 - 1 (4294967295, each of which can store more than 4 billion members).


zset (sorted set: ordered collection)

Redis zset set is a collection string type elements and does not allow duplicate members.

The difference is that each element is associated with a double score. redis members from small to large by fractions.

zset member is unique, but the (score) be repeated.

zadd command

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

zadd key score member 

Instance

redis 127.0.0.1:6379> zadd w3cschool.cn 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd w3cschool.cn 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd w3cschool.cn 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd w3cschool.cn 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE w3cschool.cn 0 1000

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