redis It's based on key value pairs , its key The type is actually object, So what is generally called redis The data type is actually value Data type of . From the big aspect , There are five main types :

* string
* list
* hash
* set
* sorted_set

string type , Should be redis One of the most commonly used , It can hold strings , You can store values , You can also store binary bitmaps bitmap.
that string What do types actually exist , It's actually an array of bytes
.redis about string There are many commands for type operations , use string The length query command can verify whether there are characters or bytes .
The validation process is , Storing Chinese characters under different character set codes , You will find that STRLEN The result is not the number of characters , It is the byte length corresponding to the corresponding character under the corresponding character set code .

string Type can store strings , Therefore, it also provides many basic and commonly used string operation functions , For example, string concatenation append, Length query strlen, Get specific position character
getrange etc .
In many related to the authority system design , Often used string Type storage token, And set a certain period of validity .

Above operation , And the following operations , Can be in redis-cli Through the help Make a query , For example, to query string Supported features , You can use it help @string.

string Type can store values , So it also provides a lot of numerical operation functions , For example, add one incr, Minus one decr, GATT value incrby, Subtract specific value decrby etc .
The amount of access to certain functions of some systems by this function , Comment volume , In online and offline business scenarios , There will be good applications .

string Type can be stored bitmap, Also provides a lot of bitmap operations , For example, set a bitmap setbit, Get bitmap data getbit, Bitmap statistics bitcount, Composite statistics
bitop etc .
bitmap It's a bit based operation , A small amount of memory can be used to represent a large amount of data , It is very practical for many statistical business , Week for example , month , Data statistics of these specific scenarios .
It should be noted that ,bitmap Medium offset There are limits , Cannot exceed Integer Maximum limit of , Otherwise, it will throw an exception , Storage failure .

string The type is set when the value is set , You can use a single operation set, You can also use multi element operations mset, Whether it's a single element operation , Is it a multi-element operation , They all have an end nx
Operation of , This operation supports nx and xx operation , One is that key If it does not exist, the value can be set successfully , One is that key The value can be set successfully only when it exists .

This function can guarantee the atomicity of data to some extent , Therefore, it is often used in distributed application scenarios .( Exactly , It ensures atomicity in multi element operation , because redis It's a single process , Single threaded , So there is no atomicity problem with single element operations )


list So is the type redis It's a very common type , stay list type key With head and tail attribute , Respectively point to list The first and last elements in , This design enables list Type can simulate a variety of commonly used data structures .

to list Store elements in , You can choose to append and push data from left or right , You can also choose linsert This operation inserts data at a specific location , You can also use lset This operation replaces data at a specific location .
The above additional push in method , There are corresponding pop-up data operations , It can be taken out in the same direction , You can also select reverse extraction .
For the same direction take out operation , Namely lpush and lpop, as well as rpush and rpop, The result of this operation is the last in first out , Just and java The design of this data structure is the same .
And reverse it , Namely lpush corresponding rpop, perhaps rpush corresponding lpop, The result of this operation is FIFO , The so-called FIFO, Just and java The data structure of squadron is the same .
and linsert,lset This kind of operation and java The operation of array structure in is the same .
meanwhile ,list Asynchronous blocking is also supported , for example blpop,brpop, This operation blocks the client , Until the corresponding key Array of , This kind of operation can realize some business requirements of unicast subscription .
list Type , Elements are allowed to be duplicated , And can be retrieved in the order of storage , It can be seen that he is in order , This order is the order in which data are stored .
All in all ,redis Of list Type can replace some java Commonly used in api And data structure , It can guarantee the reliability of data to some extent .

list Types have many operations , Not limited to those listed above , and string The same type , It can also be used help @list
Method query redis Self contained help document , The actual corresponding learning and understanding when necessary .


redis It is a key value structure , there hash refer to value It is also a key value pair , This structure is similar to java in hashmap There's another one inside hashmap.
therefore , about value Of hash Type itself , It's like redis Of string The type is the same , And in fact hash Most of the operations are also String Very similar in type .
In terms of operation command , Almost string The corresponding command is preceded by a h
character , The operation of storing data is the original operation key Add an inner layer of key, It's just that the standard name here is field.
get Access to data ,set Data storage , And the addition and subtraction of values , Duhe string It's very similar .

hash This structure of , It works well with an object , Multiple attributes , This kind of data storage . For example, some properties of popular products are stored , Data storage of a vehicle condition in Internet of vehicles , It can reduce the operation of database when hot data is used , At the same time, it can modify the data conveniently and efficiently .


In terms of data access alone ,redis in list Type and set type , And java Medium list and set It's very similar , stay java in ,list and set It's all a collection , It's all containers , Can save a lot of other things in it , The difference is that list Allow repetition , And it's orderly , and set Repetition is not allowed .
in the way of set The problem of order , stay java in set There are many subclasses , Some are out of order , Some are orderly , So we can't generalize .
and redis in , and java Similarly ,set It is also not allowed to repeat , But here it is set It's disorder .
set Duplicate features are not allowed , There are many application scenarios , For example, it can be used to count online users in a certain period of time , Users who are online many times can be removed directly .

except set In addition to the characteristics of the data type itself ,redis It also provides some useful information for this type api command , for example sinter You can take two set Intersection of data ,sdiff
You can take two set Difference set of ,sunion You can take two set Union of .
What needs to be noted here is that ,sdiff Actually, there is a direction , The one on the far left key The content shall prevail , So the actual direction needs to be controlled by yourself .
in addition , The above three operations , There's one of them store Operation of , This operation needs to provide an additional key, Will intersect , Union , The result of the difference set is stored in this extra key inside .

set Type also has a very good operation , According to the given key Generate a certain random number , The order is srandmember
, This operation , You need one key parameter , It also needs a number , That is, the number of random numbers generated .

There is something particular about this number , If it's a positive number , And less than set The number of its own elements , It will randomly return a given number of non duplicate data . If it's a positive number , And greater than set Number of elements , The data for the given data is returned , But there will be repetition . If it's negative , And less than set Number of elements , The results may be repeated , It may not be repeated . If it is negative and greater than set Number of elements , A given amount of data is also returned , There will be repetition .
The above function , Is very suitable for similar captcha , Lottery and other business scenarios .

about set, There is an operation to be noted , Namely smembers, This operation can list one key All elements under , But it's a drain redis Throughput operations , It needs to be used with caution .


sorted_set It's orderly set aggregate , In a sense ,sorted_set It's more like right list and set A supplement to .list It's orderly ( Order of deposit ), But not heavy , and set It's de duplication , But it's out of order .

Naturally, these designs have their own advantages , Each has its own application scenarios , But it's de duplicated and orderly , This demand is also very common , and sorted_set That's one type , It's just the order and list Different , Not the order of deposit , It's sort .

The above statement is actually an understanding , so to speak sorted_set yes list and set Supplement to , It can also be said to be a synthesis . because sorted_set It's more like a synthesis list and set Operation of , and set The same has intersection , Union , Difference set operation , And again list There are also blocking data fetching operations .
But there is a difference ,sorted_set Set operations in , There are also operations with weights and aggregations , You can take the maximum , Minimum and summation , It also makes it has more application scenarios .

sorted_set Is an ordered type with ordering , The sorting here needs to be given a score when storing data . This score can determine the order of the corresponding elements , But this score is not to say that a given will not change , You can add and subtract , And the score after the operation , The corresponding order will also change at the same time .
This feature is very suitable for some real-time ranking data operations , For example, song charts , Blog rankings , Sales rankings and so on .

generally speaking , With sorting, there must be additional data maintenance , It will inevitably lead to low efficiency in the same situation .
and sorted_set What's special is that , The underlying structure is storage skip
list Jump table , This structural ratio set Of key More content is stored , bring sorted_sort It has been improved in efficiency ( The specific structural details need to be supplemented )

©2019-2020 Toolsou All rights reserved,
QQ Login interface implementation code implementation mysql Addition, deletion, modification and query of database JAVA Experiment 4 set and functional programming experiment about String How to create objects VHDL——4 choose 1 Data selector C language ( Guess numbers games ) Blue Bridge Cup MCU advanced module --NE555 I don't memorize eight part essays , You go to the interview ? Hill sorting of sorting algorithm ——c++ realization python What does built-in function mean _python What are the built-in functions