Redis study Note 3 (queue function)

Source: Internet
Author: User
Tags php redis
List of redis queue Functions

Common commands:

Blpop deletes and obtains the first element in the list, or blocks it until one is available.

Brpop deletes and obtains the last element in the list, or blocks it until there is a available

Brpoplpush

Lindex obtains an element through its index list.

Linsert inserts an element before or after another element in the list.

Llen obtains the length of the queue (list ).

Lpop is an element from the left of the queue.

Lpush one or more elements from the left of the queue

When a queue exists, lpushx enters an element from the queue to the left.

Lrange obtains the specified returned element from the list.

Lrem deletes an element from the list

Lset sets the value of an element in the queue.

List of ltrim trim to a specified range

Rpop is an element that leaves the queue from the right.

Rpoplpush deletes the last element in the list and appends it to another list.

Rpush queues an element from the right of the queue.

Rpushx queues an element from the right of the queue, valid only when the queue exists

 

Redis supports interfaces such as PHP, Python, and C.

 

 

 

 

 

 

 

 

Application scenarios:

Redis list has many application scenarios and is also one of redis's most important data structures. For example, the Twitter follow list and fans list can all be implemented using the redis list structure.

Lists is a linked list. I believe that anyone with a little knowledge about data structures should be able to understand its structure. Using the lists structure, we can easily implement the latest message ranking and other functions.

Another application of lists is message queue,

The lists push operation can be used to store the task in lists, and then the working thread uses the pop operation to retrieve and execute the task. Redis also provides APIs for operations on a certain segment of lists. You can directly query and delete the elements of a segment of lists.

 

If needed, you can also use the redis sorted-sets data structure for priority queues. You can add a unique sequence number to each message. I will not go into details here.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Implementation Method:

The implementation of redis list is a two-way linked list, that is, it supports reverse lookup and traversal to facilitate operations. However, it brings some additional memory overhead and many internal implementations of redis, this data structure is also used, including the sending Buffer Queue.

 

 

 

 

 

 

 

 

 

 

 

: 1) join

 

 

 

 

 

 

 

 

 

 

 

2) team-out (non-blocking mode)

 

 

Lpop pops up the first element of the List (that is, the last element to join the queue)

 

 

 

 

 

 

Rpop pops up the end element of the List (that is, the first element of the queue)

Note: To use this queue function

 

 

This is a non-blocking mode, that is, if the queue is empty when you exit with pop, you get a null value.

 

 

 

 

 

 

 

 

 

 

 

3) team out (Blocking Mode)

If the queue is empty, use the brpop command.

Brpop is a blocking list pop-up primitive. It is the blocking version of rpop, because this command will block the connection when the given list cannot pop up any element. This command will view the list according to the given key order, and an element will pop up at the end of the first non-empty list.

 

 

 

 

A)

Run the brpop command.

It can be seen that when the queue has no elements, it is blocked, that is, it does not return values.

Where 0 indicates that the time-out period is 0, indicating that the request has been waiting.

 

B)

At this time, we use lpush to queue a data "BBB"

 

C)

The queue is immediately displayed with the queue name and the waiting time of the queue element.

 

D)

Brpop can also block multiple queues at the same time, for example

 

 

 

 

Problems with using redis list as a queue

1) the queue function becomes invalid when redis crashes.

2) if the data is always inserted at the queue end, And the queue end does not consume data, or the queue entry frequency is large and large, the slow consumption frequency at the queue end will cause the memory to soar.

3) redis queues can also be used for message persistence like rabbitmq, or message persistence.

For persistence, You need to enable the dump data function of redis. We do not recommend enabling persistence for the moment.

 

Redis is only suitable for caching, not for databases or storage. Its persistence method is suitable for saving and cannot be used as a common function. When the data volume is small, it cannot be seen. When the data volume reaches a million level and the memory size is about 10 Gb, the performance is very affected.

 

4) if multiple consumers listen to one queue at the same time, one of them leaves one element, and the other cannot obtain this element.

5) The redis queue application scenario is a one-to-many or one-to-one relationship, that is, there are multiple queues, but there is only one consumer end (out of the queue)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Simple PHP redis operation example

 

Redis study Note 3 (queue function)

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.