Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the scan of php redis

Shulou Source: shulou.com Published: 2022-06-02 07:13:04 09月11日 Update

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.

Tags: Commands articles content more cursors queries good practical code examples companies scenarios characters strings clients clients objects articles ways methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS vpn Linux Shulou Information