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 is the method for redis to delete key in batch in php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you what the php redis batch delete key method is, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Php redis's method to delete key: first iterate through the key; prefixed with [XX] using the scan command incrementally, and then delete the specified key through the unlink function.

Php redis bulk delete key

Abstract

Use the scan command to iterate incrementally through the key prefixed with XX, and then delete

Brief introduction

In actual development, some useless data will be mixed with redis carelessly, for example, some big brother forgot to add cache time, or because the data structure list needs to be changed to zset because of business modification, the previous cache is useless, but the expiration time is a bit long, so you need to delete key in batch. If you use the keys command to traverse a qualified keys, it will block other operations of the redis, which will definitely not work in the production environment, so sacrifice the scan command mass killer.

Code

/ * * in a redis cluster, the scan command needs to specify a node. It is also important to note that the primary node is connected. * * / $redis = new\ Redis (); $timeout = 2.5 [redisConf [0], $redisConf [1], [[$ip, 8001], [$ip, 8002], [$ip, 8003]]; foreach ($redisConfArr as $redisConf) {$redis- > pconnect ($redisConf [0], $redisConf [1], $timeout) / / an empty array may be returned by default SCAN_NORETRY. Set it to SCAN_RETRY. If it is an empty array, it will not be returned to continue scanning $redis- > setOption (\ Redis::OPT_SCAN,\ Redis::SCAN_RETRY); $it = NULL While ($arr_keys = $redis- > scan ($it, CacheKeyConfig::CachePre.'*')) {if (is_array ($arr_keys)) {/ / it is recommended to use the unlink function, which is non-blocking and easy to use when deleting large key, but it requires redis version > = 4.0$ result = $redis- > del ($arr_keys); echo $result. PHP_EOL;}} echo "OK!" PHP_EOL;} above are all the contents of what is the method for redis to delete key in batch in php. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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