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 use the scan of php redis

2025-02-23 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 to use the scan of php redis. 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.

When deleting the cache, we need to delete it in batch in some scenarios, but we are not sure the specific key value. We can query it by matching and delete it.

However, using keys can cause the redis server to go down. Use it cautiously.

In general, companies will also disable more sensitive commands such as keys.

So the scan command is used to match the query at work.

SCAN cursor [MATCH pattern] [COUNT count]

such as

# scan key values that match test1:* from cursor 0, scanning 1000 scan 0 match test1:* count 1000 at a time

1) represents the cursor value for the next scan, and the command line displays a string type.

2) indicates the list of key values matched by this scan

How to implement it with php code, for example

Function getKeysByPattern ($pattern) {$keysList = []; while (true) {/ / @ todo replace the client here with your own redis client object $keys = $client- > scan ($iterator, $pattern,1000); $keysList = array_merge ($keysList, $keys?? []); if ($iterator = 0) {/ / iteration ended and no key break matching pattern was found } if ($iterator = null) {/ / "cursor is null, reset to 0, continue scanning" $iterator = "0";}} $keysList = array_unique ($keysList); return keysList;}

Content expansion

Php redis extension supports the implementation of scan commands

# git clone https://github.com/phpredis/phpredis# cd phpredis# / opt/php/bin/phpize #. / configure-- with-php-config=/opt/php/bin/php-config # make & & make install's article on "how to use scan in phpredis" ends here. I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please 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

Development

Wechat

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

12
Report