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

How to set expiration time in batches by Redis

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how Redis sets the expiration time in batches. 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.

How does Redis set the expiration time in batches?

Not to mention setting the expiration time in batches through the set () function in foreach

We introduce PIPLINE of redis to solve the problem of batch setting expiration time.

What is the principle of PIPLINE?

Did not use pipline to execute N commands

Execute N commands using pipline

The principle of PIPLINE can be clearly seen from the illustration:

The client only needs to send a request through the PIPLINE splicing subcommand. After the redis receives the PIPLINE command, it processes the command block composed of PIPLINE, which reduces the number of network request responses.

The greater the network delay, the more the advantages of PIPLINE can be realized.

The more spliced subcommands, the more advantages of using PIPLINE can be reflected.

Note: it is not true that the more subcommands are stitched, the better. The N value is also limited. When the stitching command is too long, it will cause the client to wait for a long time, causing network congestion. We can split a large number of commands into several PIPLINE for execution according to the actual situation.

Code encapsulation / / batch setting expiration time public static function myPut (array $data, $ttl = 0) {if (empty ($data)) {return false;} $pipeline = Redis::connection ('cache')-> multi (\ Redis::PIPELINE); foreach ($data as $key = > $value) {if (empty ($value)) {continue } if ($ttl = = 0) {$pipeline- > set (trim ($key), $value);} else {$pipeline- > set (trim ($key), $value, $ttl);}} $pipeline- > exec ();} Project actual combat requirements description

Open APP and send my online notification to people who like me (to avoid disturbance, repeated login within 8 hours does not trigger notification)

Everyone will only receive this kind of online notification every half hour (that is, even if 10,000 people I like are online within half an hour, I will only receive one notification from the person I like)

Analysis of key points

Rational use of cache to reduce the number of DB reads and writes

Not only to reduce the number of DB read and write, but also to reduce the number of Redis read and write, using PIPLINE

Code implementation parsing

CanRecall () is elegantly written, first judging whether the tag has been sent, and then judging HouseOpen::getCurrentOpen (), because HouseOpen::getCurrentOpen () is to query DB calculation, this kind of code should be executed as little as possible, reducing DB queries.

Array_diff () takes the idea of difference set to get the people who need to push.

Encapsulation tool class

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

Development

Wechat

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

12
Report