Redis中redis-cluster如何删除指定的key-创新互联

这篇文章主要为大家展示了“Redis中redis-cluster如何删除指定的key”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Redis中redis-cluster如何删除指定的key”这篇文章吧。

创新互联建站专注于企业营销型网站、网站重做改版、昂昂溪网站定制设计、自适应品牌网站建设、html5成都商城网站开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为昂昂溪等各大城市提供网站开发制作服务。

需要从redis-cluster中删除指定的key

循环每个主节点的版本:

  1. [root@ip-172-31-39-42 ~]# more delkey-single.py

  2. # encoding: utf-8

  3. import redis

  4. import random

  5. import string

  6. import rediscluster

  7. import time

  8. 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']

  9. def del_keys_without_pipe(newhost):

  10.     pool = redis.ConnectionPool(host=newhost, port=6379, db=0,password='inH7HkD7iXoxKZCi')

  11.     r = redis.StrictRedis(connection_pool=pool)

  12.     start_time = time.time()

  13.     result_length = 0

  14.     for key in r.scan_iter(match='user:*:type:*:news:msg', count=100):

  15.         if r.ttl(key) == -1:     如果未设置过期时间

  16.            print key

  17.    #       r.delete(key)

  18.         result_length += 1

  19.     print "normal ways end at:", time.time() - start_time

  20.     print newhost,"normal ways delete numbers:", result_length

  21. for i in all_nodes:

  22.         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

链接集群版本

  1. [root@ip-172-31-39-42 ~]# more delkey-cluster.py

  2. # encoding: utf-8

  3. import redis

  4. import random

  5. import string

  6. import rediscluster

  7. import time

  8. def del_keys_without_pipe():

  9.     startup_nodes = [{"host": "172.31.39.42", "port": 6379,"db":0}]

  10.     r = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, password='inH7HkD7iXoxKZCi')

  11.     start_time = time.time()

  12.     result_length = 0

  13.     for key in r.scan_iter(match='user:*:type:*:news:msg', count=100):

  14.     #        r.delete(key)

  15.         result_length += 1

  16.     print "normal ways end at:", time.time() - start_time

  17.     print "normal ways delete numbers:", result_length

  18. del_keys_without_pipe()

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

当前标题:Redis中redis-cluster如何删除指定的key-创新互联
分享地址:https://www.cdcxhl.com/article26/ccsdjg.html

成都网站建设公司_创新互联,为您提供小程序开发云服务器网站改版全网营销推广定制开发网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司