Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What can redis be used for?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what redis can be used to do. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Redis can be used not only as a cache server, but also as a message queue. Its list type is inherently supported as a message queue.

Because the list of Redis is implemented using a two-way linked list, saving the head and tail nodes, it is very fast to insert elements on both sides of the list.

So you can use Redis's List to implement message queues directly, with two simple instructions, lpush and rpop, or rpush and lpop.

However, there will be a problem for message consumers, that is, they need to constantly call the rpop method to check whether messages are waiting to be processed in the List. Each call initiates a connection, which can cause unnecessary waste. You might use methods such as Thread.sleep () to get consumer threads to spend again at regular intervals, but there are two problems with doing so:

1) if the speed of the producer is faster than that of the consumer, the length of the message queue will increase all the time, and it will take up a lot of memory space over time.

2) if the sleep time is too long, it can not deal with some timely messages, and if the sleep time is too short, it will also cause a lot of overhead on the connection.

So you can use the brpop instruction, which returns only if there is an element, and if it doesn't, it blocks until the timeout returns null, and then you can run Customer, empty the console, and you can see that the program has no output and is blocked here in brpop. Then in the client that opens Redis, enter the instruction client list to see that there are currently two connections.

In addition to supporting message queuing, Redis provides a set of commands to support the publish / subscribe mode.

1) publish

The PUBLISH directive can be used to publish a message in the format PUBLISH channel message

The return value indicates the number of subscriptions to the message.

2) subscribe

The SUBSCRIBE instruction is used to receive a message in the format SUBSCRIBE channel

You can see that you entered subscription mode after using the SUBSCRIBE instruction, but did not receive the message sent by publish, because the subscription received it only before the message was sent. For other instructions in this mode, you can only see the reply.

There are three types of responses:

1. If subscribe, the second value represents the subscribed channel, and the third value indicates the number of subscribed channels? (understood as a serial number?)

2. If message (message), the second value is the channel that generated the message, and the third value is the message

3. In the case of unsubscribe, the second value represents the unsubscribed channel, and the third value represents the number of subscriptions to the current client.

You can use the instruction UNSUBSCRIBE to unsubscribe. If no parameter is added, all channels subscribed by the SUBSCRIBE directive will be unsubscribed.

Redis also supports wildcard-based message subscriptions using the directive PSUBSCRIBE (pattern subscribe).

You can see that the publish instruction returns 2, while the subscriber side receives the message twice. This is because the PSUBSCRIBE instruction can repeat subscription channels. Channels subscribed with the PSUBSCRIBE instruction also use the instruction PUNSUBSCRIBE instruction to unsubscribe, which cannot unsubscribe to the channel subscribed by SUBSCRIBE, and similarly, UNSUBSCRIBE cannot unsubscribe to the channel subscribed by the PSUBSCRIBE instruction. At the same time, PUNSUBSCRIBE instructs that wildcards are not expanded.

Summary:

A message queue can be made simply and quickly by using the List data structure of Redis. At the same time, the instructions such as BRPOP and BLPOP provided by Redis solve the problem of waste of resources caused by frequent calls to rpop and lpop methods of Jedis. In addition, Redis provides instructions for publish / subscribe mode, which can achieve message delivery and inter-process communication.

On what redis can be used to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report