温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Redis批量删除key以及setnx使用场景的分析

发布时间:2021-10-20 18:17:46 来源:亿速云 阅读:452 作者:柒染 栏目:大数据

这篇文章给大家介绍Redis批量删除key以及setnx使用场景的分析,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1, 批量删除key

redis中要删除某个key的值,只能通过del key的方式进行删除。如果要批量删除呢?很遗憾,Redis并不支持按key的正则方式进行删除的操作。那如何去达到这个效果呢?可以使用linux的管道命令,如下:

/usr/local/redis/bin/redis-cli keys abc:* | xargs /usr/local/redis/bin/redis-cli del

前面的/usr/local/redis/bin/redis-cli是redis的客户端,keys abc:* 表示获取到以abc:开头的所有key,然后把这些key,当做参数(xargs),传递给管道后面的del命令。这样就实现了批量删除key的功能。但是如果key的前缀后面接着就是空格,那这个正则就无法命中了。

当然,在redis命令后面,还可以加上很多其他的参数,比如:

-h ip //表示某个ip主机
-p port //表示该主机端口
-a password //表示连接密码
-n 1 //表示选择节点号为1的数据库节点(不写默认连接为0),对应的命令为 select 1

比如

/usr/local/redis/bin/redis-cli -n 1 keys "ab:token*" | xargs /usr/local/redis/bin/redis-cli -n 1 del
(integer) 188 //表示删除了188个key

注意管道前后都加了相同的参数,否则会匹配不上。

当然,如果要全部删除某个节点的数据,可以直接使用

flushdb
flushall

这两个命令。

2, redis 的 expire key time_in_seconds 命令,后面的过期时间,必须是整数,不能为小数。否则会报 ERR value is not an integer or out of range 错误。

3,redis的setnx使用场景

<?php

public function abc() {
    //唯一key
    $key = 'string_key_'.$uniqueId;
    //redis实例
    $redis = app('redis')->connection('cache');
    $random = rand(1, 10000);
    //将唯一key赋值为$random值,如果成功,表示加上了锁,如果未成功,等待锁释放,直到获取到锁
    while (!$locked = $redis->setnx($key, $random)) {
        sleep(0.05);
    }
    //设置成功后加过期时间
    $ttl = 1;
    $redis->expire($key, $ttl);

    //业务逻辑处理
    
    //如果业务逻辑处理完,为防止del把其他请求的锁删掉,这里加上$random值校验,确保这里删除的锁是本次周期内的
    if ($redis->get($key) == $random) {
        $redis->del([$key]);
    }
    
    //返回结果
    return $result;
}

这里要注意的是,如果业务处理逻辑未处理完,此时key失效了,那第二个请求过来,它可以直接获取到一个新的锁,然后执行一个新的业务逻辑,造成实际上的并发。所以,这里的 过期时间,一定要设置得比 业务处理逻辑更长一些,让业务逻辑处理完。

4,对redis进行连接限制保护时,有三部分:

  • 在redis配置文件中的protected-mode 设置,yes 表示 以保护模式运行,需要配置 bind ip 或者需要设置访问密码(requirepass);

  • bind ip 绑定IP 配置,它限制的是哪些IP才可以连接本redis,其中0.0.0.0是允许所以外网连接,当然127.0.0.1就是本机;

  • requirepass 设置访问密码;

有四种方式可以改变保护模式,如下

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 
//如果你想要从外部电脑连接Redis,你可能需要采用以下方案中的一种
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside

关于Redis批量删除key以及setnx使用场景的分析就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI