分享更有价值
被信任是一种快乐

Redis中redis-cluster如何删除指定的key

文章页正文上

这篇文章主要为大家展示了“Redis中redis-cluster如何删除指定的key”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Redis中redis-cluster如何删除指定的key”这篇文章吧。[root@ip-172-31-39-42 ~]# more delkey-single.py
# encoding: utf-8

import redis
import random
import string
import rediscluster
import time
all_nodes=[‘172.31.39.42′,’172.31.36.10′,’172.31.47.157′,’172.31.36.49′,’172.31.40.169′,’172.31.32.234’]

def del_keys_without_pipe(newhost):
pool = redis.ConnectionPool(host=newhost, port=6379, db=0,password=’inH7HkD7iXoxKZCi’)
r = redis.StrictRedis(connection_pool=pool)
start_time = time.time()
result_length = 0
for key in r.scan_iter(match=’user:*:type:*:news:msg’, count=100):
if r.ttl(key) == -1: 如果未设置过期时间
print key
# r.delete(key)
result_length += 1
print “normal ways end at:”, time.time() – start_time
print newhost,”normal ways delete numbers:”, result_length
for i in all_nodes:
del_keys_without_pipe(i)

注意:循环链接只能去链接主节点,如果链接从节点会报以下的错误:
>>> for key in r.scan_iter(count=10):
… if r.ttl(key) == -1:
… print key

Traceback (most recent call last):
File “”, line 2, in
File “/usr/lib/python2.7/site-packages/redis/client.py”, line 1231, in ttl
return self.execute_command(‘TTL’, name)
File “/usr/lib/python2.7/site-packages/redis/client.py”, line 668, in execute_command
return self.parse_response(connection, command_name, **options)
File “/usr/lib/python2.7/site-packages/redis/client.py”, line 680, in parse_response
response = connection.read_response()
File “/usr/lib/python2.7/site-packages/redis/connection.py”, line 629, in read_response
raise response
redis.exceptions.ResponseError: MOVED 12752 172.31.32.234:6379

[root@ip-172-31-39-42 ~]# more delkey-cluster.py
# encoding: utf-8

import redis
import random
import string
import rediscluster
import time

def del_keys_without_pipe():
startup_nodes = [{“host”: “172.31.39.42”, “port”: 6379,”db”:0}]
r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password=’inH7HkD7iX免费主机域名oxKZCi’)免费主机域名
start_time = time.time()
result_length = 0
for key in r.scan_iter(match=’user:*:type:*:news:msg’, count=100):
# r.delete(key)
result_length += 1
print “normal ways end at:”, time.time() – start_time
print “normal ways delete numbers:”, result_length

del_keys_without_pipe()以上是“Redis中redis-cluster如何删除指定的key”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注云技术行业资讯频道!

相关推荐: MySQL如何实现用户密码过期功能

这篇文章主要介绍了MySQL如何实现用户密码过期功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 从MySQL版本5.6.6版本起,添加了password_expired功能,它允许设置用户的…

文章页内容下
赞(0) 打赏
版权声明:本站采用知识共享、学习交流,不允许用于商业用途;文章由发布者自行承担一切责任,与本站无关。
文章页正文下
文章页评论上

云服务器、web空间可免费试用

宝塔面板主机、支持php,mysql等,SSL部署;安全高速企业专供99.999%稳定,另有高防主机、不限制内容等类型,具体可咨询QQ:360163164,Tel同微信:18905205712

主机选购导航云服务器试用

登录

找回密码

注册