Get the App
SLTechnology News&Howtos  ›  Database  › 

How does redis-cluster delete a specified key in Redis

Shulou Source: shulou.com Published: 2022-05-31 15:15:58 10月04日 Update

This article mainly shows you the "Redis redis-cluster how to delete the specified key", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Redis redis-cluster how to delete the specified key" this article.

You need to remove the version of each master node of the specified key loop from redis-cluster:

[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)

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: if expiration time is not set

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)

Note: circular links can only link to the master node, if the link slave node will report the following error:

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

Link Cluster version

[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 ()

The above is all the content of the article "how to delete the specified key in redis-cluster in Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Links content articles nodes versions learning help looping time easy to understand more organization knowledge editing industry information information channels errors clusters channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Linux Shulou Information vpn