Redis topic (5)-5 common data types in Redis

1 Introduction

Redis key may also be stored in addition to store five kinds of common data types, are: String, List, Set, Hash, ZSet. For Redisthe command part it is common, but there are some other commands that belong to a special use.

First look at a Rediscomparison of the five data structures:
Insert picture description here

2.String string type

The String type in Redis is a sequence of bytes, which is very similar to the string operations provided by other programming languages ​​or other key-value pair storage.

One example of the type String, where the key to hello, and a value of World:
Insert picture description here
(. 1) common commands follows:
Insert picture description here
(2) in the Redis command increment and decrement commands:
Insert picture description here
(3) In addition get, , set, delincrement, decrement operations like In addition, Redisthe following operations are also provided:

1、获取字符串长度
2、往字符串append内容
3、设置和获取字符串的某一段内容
4、设置及获取字符串的某一位(bit)
5、批量设置一系列字符串的内容

As shown in the figure below:
Insert picture description here
(4) Application scenarios:

StringIt is the most commonly used data type, and ordinary key/valuestorage can be classified into this category. valueIn fact String, it can be not only a number , but also a number: for example, you want to know when an IP address is blocked (accessed more than a few times). INCRBYCommands make this easy, by maintaining the atomic increment

3.List list type

RedisIn Listfact, the list ( redisusing a linked list implementation of the double-ended List), I believe the learned data structure knowledge of the people should be able to understand its structure.

Using the List structure, we can easily implement the latest news ranking and other functions (such as Sina Weibo's TimeLine). Another application of List is the message queue. You can use the PUSH operation of the List to store tasks in the List, and then the worker thread will use the POP operation to take out the tasks for execution.

An instance of the List type, the elements contained in the list, the same elements can be repeated: It
Insert picture description here
should be noted that: a List structure can store multiple strings in an orderly manner, and element repetition is allowed.

(1) Common commands are as follows:
Insert picture description here
LPUSHand RPUSHcommands are used to push elements into the left and right ends of the list respectively ; LPOPand RPOPcommands are used to pop elements from the left and right ends of the list, that is, delete elements;

For the LRANGEcommand, the start index of the 0-bit range is used, and -1 is the end index of the range, and all elements in the list can be taken out.

(2) In addition to the more commonly used commands above, Redis lists can also remove elements from the list, insert elements into the middle of the list, trim the list to a specified length, and some other commands.

(3) Usage scenarios:

微博 TimeLine
消息队列

4.Set collection type

RedisBoth the collection and the list can store multiple strings. The difference between them is that the list can store multiple identical strings, and the collection uses a hash table to ensure that each string it stores is different.

Redis collections use an unordered way to store elements, so it is not possible to push elements into one end of the collection or pop elements from one end of the collection like a List list.

An instance of the Set collection type, with different elements, arranged in disorder:
Insert picture description here
(1) Common commands are as follows:
Insert picture description here
(2) In addition to common commands, there are calculations of intersection, union, and difference, as follows:
Insert picture description here
(3) )scenes to be used:

共同好友

5.Hash type

Redis hash can store the mapping between multiple key-value pairs. Like character strings, the value stored in the hash can be either a character string or a numeric value, and the user can also perform an increment operation or decrement operation on the number stored in the hash.

An example of the List hash type is a hash key that contains two key-value pairs:
Insert picture description here
(1) Common commands are as follows:

(2) Other commands include commands to add and delete key-value pairs, and commands to get all key-value pairs , And the commands for auto-increment and auto-decrement operations on the value of the key-value pair, as shown below:
Insert picture description here

6. Redis ordered collection ZSet data type

An ordered set is the same as a hash, used to store key-value pairs; the key of an ordered set is called a member, and each member is unique; and the value of an ordered set is called a score, and the score must be Is a floating point number.

Ordered set is the only structure in Redis that can access elements based on members, and access elements based on scores and the order of scores.

An example of an ordered set type, zset-key is an ordered set key containing two elements:
Insert picture description here
1) Common commands are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/BruceLiu_code/article/details/115013394