Laravel-删除所有包含特定字符串的缓存/ Redis键 [英] Laravel - Erase all cache / redis keys that contain a specific string

查看:723
本文介绍了Laravel-删除所有包含特定字符串的缓存/ Redis键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Laravel擦除与特定字符串匹配的Redis中的所有键?例如,如果我想忘记带有单词product的键,它将忘记以下键:

Is it possible to erase all keys in redis using Laravel that match a specific string? For instance, if I wanted to forget keys that have the word products, it would forget the following keys:

laravel:896235872385237588327582370375acaca235325:products-list
laravel: 896235872385237588327582370375acaca235325:products-categories

laravel:896235872385237588327582370375acaca235325:products-list laravel:896235872385237588327582370375acaca235325:products-categories

我可以在redis服务器上运行它,但是我想知道是否可以通过Laravel直接运行更好的东西:

I would run this on the redis server but I'm wondering if there's anything better that could be run via Laravel directly:

redis-cli键 * | grep产品 | xargs redis-cli DEL

推荐答案

为什么不呢?

public function forget($key_name)
{
    $redis = Cache::getRedis();
    $keys = $redis->keys("*$key_name*");
    $count = 0;
    foreach ($keys as $key) {
        $redis->del($key);
        $count++;
    }      
    return $count;  
}

对于生产环境,SCAN光标[MATCH模式] [COUNT计数]应为而不是键

And for production environment SCAN cursor [MATCH pattern] [COUNT count] should be used instead of "keys"

http:// redis。 io / commands / scan

因为您不想在Redis中有成千上万个键的情况下用完内存,而您不会不想通过使用键来阻止整个redis实例。

Because you don't want to run out of memory in case you have tens of thousands keys in redis, and you don't want to block your whole redis instance by using "keys".

这篇关于Laravel-删除所有包含特定字符串的缓存/ Redis键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆