Redis Common data types

Source: Internet
Author: User
Tags set set live chat

String
    1. Common commands:
    2. Get, set, INCR, Decr mget, and so on, ordinary key/value storage can be classified as such
Hash
    1. Common commands:
    2. Hget,hset,hgetall and so on.
List (Queue)
    1. Common commands:
    2. Lpush,rpush,lpop,rpop,lrange,blpop (blocked version) and so on.
    3. 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
    1. Common commands:
    2. Sadd,srem,spop,sdiff, Smembers,sunion and so on.
    3. Application Scenarios:
    4. 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.
    5. 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.
    6. Redis also provides a set of intersection, set, and difference sets for the collection, which can be very convenient for real
Sort Set

  

  1. Common commands:
  2. Zadd,zrange,zrem,zcard, etc.
  3. Usage scenarios:
  4. A condition is a weight, such as the number of times the top is sorted.
  5. 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.
  6. 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.
  7. For example, the public timeline of Twitter can be stored as score in the publication time, which is automatically sorted by time.
  8. 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.
  9. 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.
  10. Applications that require precise setting of expiration time
  11. 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.
  12. Implementation method:
  13. 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

  

    1. Pub/sub
    2. Pub/sub literally is the release (Publish) and Subscription (Subscribe), in Redis, you can set a key value for message publishing and message subscription,
    3. 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.
    4. Client 1:subscribe Rain
    5. Client 2:publish Rain "My Love!!!"
    6. (integer) 2 indicates that several clients have subscribed to this message
Transaction
    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.