Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[Redis] use python to operate redis cluster

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

https://blog.csdn.net/bitcarmanlee/article/details/51852126

Passwords cannot be written to the list:

redisconn = strictRedisCluster(startup_nodes=redis_nodes, password=password)

Combined with the previous article, improve the script operation redis-cluster

[root@ip-172-31-47-226 tmp]# more /root/get_nottl.py

# encoding: utf-8

"""

author: yangyi@youzan.com

time: 2018/4/26 PM 4:34

func: Get key without ttl set in database

"""

import redis

import argparse

import time

import sys

import rediscluster

class ShowProcess:

"""

Class showing processing progress

The display of processing progress can be realized by calling the related functions of this class

"""

i = 0 #Current processing progress

max_steps = 0 #Total number of times to process

max_arrow = 50 #Length of progress bar

#Initialize the function, you need to know the total number of processing times

def __init__(self, max_steps):

self.max_steps = max_steps

self.i = 0

#Show function, show progress according to current processing progress i

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

def show_process(self, i = None):

if i is not None:

self.i = i

else:

self.i += 1

num_arrow = int(int(self.i) * int(self.max_arrow) / int(self.max_steps)) #Calculate how many '>' are displayed

num_line = self.max_arrow - num_arrow #Calculate how many '-' are displayed

percent = self.i * 100.0 / self.max_steps #Calculate completion progress in xx.xx%

process_bar = '[' + '>' * num_arrow + ' ' * num_line + ']'+ '%.2f' % percent + '%' + '\r' #string with output,'\r' means no line break back to the far left

sys.stdout.write(process_bar) #Print these two sentences to the terminal

sys.stdout.flush()

def close(self, words='done'):

print ''

print words

self.i = 0

def check_ttl(redis_conn, no_ttl_file, dbindex):

start_time = time.time()

no_ttl_num = 0

allkey = redis_conn.dbsize()

keys_num = sum(list(set(allkey.values())))

print "key is distributed as follows:",allkey

print "there are {num} keys in db {index} ".format(num=keys_num, index=dbindex)

process_bar = ShowProcess(keys_num)

with open(no_ttl_file, 'a') as f:

for key in redis_conn.scan_iter(count=10):

process_bar.show_process()

if redis_conn.ttl(key) == -1:

no_ttl_num += 1

if no_ttl_num

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report