Strings The data structure is simple key-value type ,value It's not just String, It can also be numbers . Use Strings type , You can fully achieve the present Memcached The function of , And more efficient . You can also enjoy Redis Time persistence of , Operation log and Replication And so on . In addition to providing with Memcached Same get、set、incr、decr And so on ,Redis The following operations are also provided ：
stay Memcached in , We often package structured information into hashmap, The value stored as a string after client serialization , For example, the user's nickname 、 Age 、 Gender 、 Integral, etc. , When you need to modify one of them , Usually all values need to be taken out and deserialized , Modify the value of an item , Re serialize the store back . This not only increases the cost , It is also not suitable for some situations where concurrent operations may occur （ For example, two concurrent operations need to modify integral ）. and Redis Of Hash Structure can make you look like in a database Update Only one property value can be modified for one property .
Lists It's a list , People who believe in a little knowledge of data structure should be able to understand its structure . Use Lists structure , We can easily achieve the latest news ranking and other functions .Lists Another application of is message queuing , You can use Lists Of PUSH operation , Keep the task alive Lists in , Then the worker thread uses POP The operation takes the task out for execution .Redis It also provides operations Lists Of or relating to api, You can check directly , Delete Lists The element of a paragraph in .
Sets It's just a set , The concept of a set is a set of distinct values . utilize Redis Provided Sets data structure , It can store some aggregate data , For example, in microblog applications , You can put all the followers of a user in a collection , Put all its fans in a collection .Redis It also provides intersection for sets 、 Combine 、 Subtraction and so on , Can be very convenient to achieve such as common concern 、 Common preferences 、 Second time friends and other functions , For all of the above set operations , You can also use different command Choose whether to return the results to the client or save them to a new collection .
and Sets comparison ,Sorted Sets Added a weight parameter score, To enable elements in a collection to press score Arrange in order , For example, one that stores the scores of the whole class Sorted Sets, Its collection value It can be the student number of a classmate , and score It can be the test score , So when the data is inserted into the set , It's a natural sequence . It can also be used Sorted Sets To make a weighted queue , For example, ordinary news score by 1, Important news score by 2, Then the worker thread can choose to press score To get the task . Give priority to important tasks .
Pub/Sub Literally, it's publishing （Publish） With subscription （Subscribe）, stay Redis in , You can set to a certain key Value for message publishing and message subscription , When one key After the news is released on the value , All clients that subscribe to it will receive the corresponding message . The most obvious use of this feature is as a real-time messaging system , For example, ordinary instant chat , Group chat and other functions .
Who says NoSQL No support for transactions , although Redis Of Transactions What is offered is not strictly ACID The business of （ For example, a string is used EXEC Submit the order for execution , Server down during execution , Then some of the orders will be executed , The rest is not implemented ）, But this Transactions It also provides the basic command package execution function （ In case of no problem with the server , It can guarantee that a series of orders are executed in sequence , There will be other client commands inserted to execute ）.Redis One is also provided Watch function , You can have a key Conduct Watch, And then execute Transactions, In the process , If this Watched The value of is modified , So this Transactions Will find and refuse to execute .
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .