Delete Redis Keys matching a pattern Laravel 5.7
You can't delete by pattern. But you can get all the keys by this pattern and then delete them:
Redis::del(Redis::keys('products:*'));
See more here.
Remove All Keys from Redis in PHP
If you have the redis connection you can simply do $redis->flushDB();
to delete all keys from the selected database.
Or if you are using laravel framework you could also do Redis::flushDB();
How to atomically delete keys matching a pattern using Redis
Starting with redis 2.6.0, you can run lua scripts, which execute atomically. I have never written one, but I think it would look something like this
EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:[YOUR_PREFIX e.g delete_me_*]
Warning: As the Redis document says, because of performance maters,
keys
command should not use for regular operations in production, this
command is intended for debugging and special operations. read
more
See the EVAL documentation.
PHP Predis: how to get/delete keys containing special characters?
Solved. The problem was related to the fact that predis insert automatically a configured prefix at the beginning of each key (in my case 'vir3_data_cache') before doing any operation. But the keys("*")
command does not strip off the prefix from the key.
So I need to do the following in order to make my code work:
$prefix = $this->redis->getOptions()->__get('prefix')->getPrefix();
$keys = $this->redis->keys("*");
$removed = 0;
foreach ($keys as $key) {
if (substr($key, 0, strlen($prefix)) == $prefix) {
$key = substr($key, strlen($prefix));
}
}
Select all key data for a specific pattern from Redis using PHP
The simplest route to SET
you can take for a better performance is to PIPE
the keys and hit the redis server once to execute all of them instead of a trip/key .
https://github.com/phpredis/phpredis/issues/251
$pipeline = $redis->multi($host, Redis::PIPELINE);
//put result in our shared list
foreach ($items as $item) {
$pipeline->sAdd($key, $item);
}
$ret = $pipeline->exec();
At the same time, there is also libraries out there if you are seeking a different way to trasnlate commands to Redis Protocol .
redis bulk import using --pipe
Related Topics
Php How to Determine the First and Last Iteration in a Foreach Loop
How to Count Columns With the Same Value in a Specific Row in MySQL
How to Fetch Record of Many Ids on Single Query
Contact Form 7 Cause Http 500 Error
How to Fix Warning Illegal String Offset in PHP
Nginx Error Recv() Failed (104: Connection Reset by Peer)
Session Destroy When One Browser Tab Gets Closed
How to Remove Accents from Characters in a PHP String
How to Prevent SQL Injection in PHP
Are PHP Short Tags Acceptable to Use
Calculate Size in Bytes of Json Payload Including It in the Json Payload in PHP
How to Delete Multiple Redis Keys With the Same Pattern in PHP Using Phpredis
Laravel Composer Sees Wrong PHP Version
Creating Advanced Search Query With PHP Mysqli
Dompdf Remote Image Is Not Displaying in Pdf
Group Array Data on One Column and Sum Data from Another Column