String
- Common commands:
- Get, set, INCR, Decr mget, and so on, ordinary key/value storage can be classified as such
Hash
- Common commands:
- Hget,hset,hgetall and so on.
List (Queue)
- Common commands:
- Lpush,rpush,lpop,rpop,lrange,blpop (blocked version) and so on.
- Application Scenarios:
Redis list has many applications and is one of the most important data structures for Redis.
We can easily achieve the latest news ranking and other functions.
Another application of the lists is Message Queuing, which can take advantage of the lists push operation to present the task in lists, and then the worker thread then takes the task out of execution with a pop operation.
Implementation method:
The implementation of Redis list is a doubly linked list, which can support reverse lookup and traversal, but it is more convenient to operate, but it brings some additional memory overhead, and many implementations within Redis, including sending buffer queues, are also used in this data structure.
Set
- Common commands:
- Sadd,srem,spop,sdiff, Smembers,sunion and so on.
- Application Scenarios:
- The functionality provided by Redis set externally is a list-like feature, except that set is automatically weight-saving, and set is a good choice when you need to store a list of data and you don't want duplicate data. and set provides an important interface to determine whether a member is within a set set, which is not available in list.
- For example, in Weibo applications, everyone's friends have a collection (set), so that two people's common friend's operation, you may only need to use the intersection command.
- Redis also provides a set of intersection, set, and difference sets for the collection, which can be very convenient for real
Sort Set
- Common commands:
- Zadd,zrange,zrem,zcard, etc.
- Usage scenarios:
- A condition is a weight, such as the number of times the top is sorted.
- The Zrevrange command can be used to get the top 100 users according to the score, Zrank can be used to get the user rank, very straightforward and easy to operate.
- The usage scenario for Redis sorted set is similar to set, except that the set is not automatically ordered, and the sorted set can be ordered by the user with an additional priority (score) parameter, and is inserted in an orderly, automatic sort.
- For example, the public timeline of Twitter can be stored as score in the publication time, which is automatically sorted by time.
- For example: The class students sortedsets,value can be the student's school number, and score can be its test scores, so that the data into the collection, has been a natural sort.
- Also can use sorted sets to do with the weight of the queue, such as the normal message score is 1, the important message of the score is 2, and then the worker can choose to press score reverse order to get work tasks. Let important tasks take precedence.
- Applications that require precise setting of expiration time
- For example, you can set the score value of the sorted set to the timestamp of the expiration time, then you can simply sort through the expiration time, and periodically purge out-of-date data, not only to clear the expired data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly.
- Implementation method:
- Redis sorted set internal use HashMap and jump Table (skiplist) to ensure the storage and ordering of data, HashMap in the member to score mapping, and the jumping table is all the members, sorted by HashMap in the score , the use of the structure of the jumping table can obtain a relatively high efficiency of finding, and it is relatively simple to implement.
Message Subscription
- Pub/sub
- Pub/sub literally is the release (Publish) and Subscription (Subscribe), in Redis, you can set a key value for message publishing and message subscription,
- When a message is published on a key value, all clients subscribing to it receive the corresponding message. The most obvious use of this function is to use it as a real-time messaging system, such as regular live chat, group chat, and other functions.
- Client 1:subscribe Rain
- Client 2:publish Rain "My Love!!!"
- (integer) 2 indicates that several clients have subscribed to this message
Transaction
- Redis also provides a watch function, you can watch a key, and then execute transactions, in the process, if the value of this watched is modified, then this transactions will find and refuse to execute.
Session 1
(1) Step 1th
Redis 127.0.0.1:6379> Get Age
"10"
Redis 127.0.0.1:6379> Watch Age
Ok
Redis 127.0.0.1:6379> Multi
Ok
Redis 127.0.0.1:6379>
Session 2
(2) Step 2nd
Redis 127.0.0.1:6379> Set Age
Ok
Redis 127.0.0.1:6379> Get Age
"30"
Redis 127.0.0.1:6379>
Session 1
(3) Step 3rd
Redis 127.0.0.1:6379> Set Age
QUEUED
Redis 127.0.0.1:6379> Exec
(nil)
Redis 127.0.0.1:6379> Get Age
"30"
Redis 127.0.0.1:6379>
The first step, Session 1 has not been time to modify the value of age
In the second step, Session 2 has set the age value to 30
In the third step, session 1 wants to set the value of age to 20, but the result of an execution return is nil, stating that execution failed, and then we take the age value is 30, which is due to the optimistic lock on age in Session 1.
Redis Common data types