[Redis] get the key script with no ttl set
Foreword in the operation and maintenance of Redis, always encounter the use of non-standard business design, such as not setting ttl on the key, resulting in a shortage of memory space, the usual solution is to dump all the key on the slave, and then traverse the files and analyze them. When you encounter dozens of gigabytes of Redis instances, dump + analysis will be a time-consuming operation. For this reason, I developed a small script that directly connects Redis to scan to traverse all key, and then checks the ttl of key and outputs the key without ttl to the specified file.
Two code implementation # encoding: utf-8
"
Author: yangyi@youzan.com
Time: 4:34 on 2018-4-26
Func: get the key with no ttl set in the database
"
Import redis
Import argparse
Import time
Import sys
Class ShowProcess:
"
A class that displays the progress of processing
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 times it is processed.
Def _ _ init__ (self, max_steps):
Self.max_steps = max_steps
Self.i = 0
# display function to display the progress according to the current processing progress
# the effect is [>] 100.00%
Def show_process (self, I = None):
If i is not None:
Self.i = I
Else:
Self.i + = 1
Num_arrow = int (self.i * self.max_arrow / 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 the progress of completion in xx.xx% format
Process_bar ='['+'>'* num_arrow +'* num_line +']'\
+'% .2f'% percent +'%'+'\ r' # string with output,'\ r' means no line wrapping goes back to the far left
Sys.stdout.write (process_bar) # these two sentences print characters 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
Keys_num = redis_conn.dbsize ()
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=1000):
Process_bar.show_process ()
If redis_conn.ttl (key) =-1:
No_ttl_num + = 1
If no_ttl_num