Five data structures of Redis

Insert picture description here
For redis, all keys are strings.

1. String

String string type is redis 最基本的数据类型, a key corresponds to a value.

The String type is binary safe, meaning redis 的 string 可以包含任何数据. Such as numbers, strings, jpg images or serialized objects.

Examples:
get, set, del, incr, decr, etc.

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
"103"
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
"102"

Actual combat scene:

(1) Cache: In classic usage scenarios, put commonly used information, strings, pictures or videos in redis, redis as the caching layer, and mysql as the persistence layer to reduce the read and write pressure of mysql.

(2) Counter: Redis is a single-threaded model, one command is executed before the next is executed, and the data can be sent to other data sources in one step.

(3) Session: common solutions spring session + redis realize session sharing.

2. Hash (hash)

Hash is a Mapmap, in turn, refers to a value of a key structure, such as value={ {field1,value1},......fieldN,valueN}}
Insert picture description here
the example:
All the commands are hash h beginning hget, hset, hdel etc.

127.0.0.1:6379> hset user name1 hao
(integer) 1
127.0.0.1:6379> hset user email1 hao@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "[email protected]"
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
"hao"
127.0.0.1:6379> hset user name2 xiaohao
(integer) 1
127.0.0.1:6379> hset user email2 xiaohao@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "[email protected]"
5) "name2"
6) "xiaohao"
7) "email2"
8) "[email protected]"

Actual combat scene:

Cache: It is intuitive, saves space compared to string, and maintains cache information, such as user information and video information.

3. Linked list

To put it plainly, List is a linked list (redis uses a double-ended linked list to implement List), which is ordered, and the value can be repeated. The corresponding value can be retrieved by subscript, and data can be inserted and deleted on both sides.
Insert picture description here
Tips for using lists:

lpush+lpop=Stack (stack)
lpush+rpop=Queue (queue)
lpush+ltrim=Capped Collection (limited collection)
lpush+brpop=Message Queue (message queue)

Example:

127.0.0.1:6379> lpush mylist 1 2 ll ls mem
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "mem"
2) "ls"
3) "ll"
4) "2"
5) "1"
127.0.0.1:6379>

Actual combat scenario:
timeline: For example, the timeline of Weibo. Someone publishes Weibo and uses lpush to join the timeline to display new list information.

4. Set collection

The collection type is also used to store the elements of multiple strings, but it is different from the list in the collection

(1) Duplicate elements are not allowed.
(2) The elements in the set are out of order, and the elements cannot be obtained through index subscripts.
(3) Operations between sets are supported. Multiple sets can be taken to take intersection, union, and difference.

Insert picture description here
Example: The
commands are sset, srem, scar, smembers, sismember starting with s

127.0.0.1:6379> sadd myset hao hao1 xiaohao hao
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) "xiaohao"
2) "hao1"
3) "hao"
127.0.0.1:6379> SISMEMBER myset hao
(integer) 1

Actual combat scene:

(1) Tag, adding tags to users, or users adding tags to messages, so that people with the same or similar tags can recommend things or people to follow.
(2) Like, or click, favorite, etc., can be put in the set to achieve

5. Zset ordered collection

Ordered collections and collections have an inevitable connection, retaining the feature that collections cannot have duplicate members. The difference is that the elements in an ordered collection can be sorted, and it sets a score for each element as the basis for sorting.

(The elements in the ordered set cannot be repeated, but the score can be repeated, just like the student ID of a classmate, but the test score can be the same).
Insert picture description here
Example:
The commands for ordered set start with z zadd, zrange, zscore

127.0.0.1:6379> zadd myscoreset 100 hao 90 xiaohao
(integer) 2
127.0.0.1:6379> ZRANGE myscoreset 0 -1
1) "xiaohao"
2) "hao"
127.0.0.1:6379> ZSCORE myscoreset hao
"100"

Actual combat scene:

Ranking: An orderly collection of classic usage scenarios.
For example, websites such as novel videos need to rank the novel videos uploaded by users. The list can be ranked according to the number of users' attention, update time, and word count.

Guess you like

Origin blog.csdn.net/weixin_43520450/article/details/107542922