這篇文章主要為大家展示了“Redis中redis-cluster如何刪除指定的key”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Redis中redis-cluster如何刪除指定的key”這篇文章吧。
創新互聯建站專注于企業營銷型網站、網站重做改版、昂昂溪網站定制設計、自適應品牌網站建設、html5、成都商城網站開發、集團公司官網建設、成都外貿網站建設公司、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為昂昂溪等各大城市提供網站開發制作服務。[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='inH7HkD7iXoxKZCi')
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”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯-成都網站建設公司行業資訊頻道!
當前標題:Redis中redis-cluster如何刪除指定的key-創新互聯
分享地址:http://vcdvsql.cn/article26/ccsdjg.html
成都網站建設公司_創新互聯,為您提供小程序開發、云服務器、網站改版、全網營銷推廣、定制開發、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯