Get the App
SLTechnology News&Howtos  ›  Servers  › 

Redis cleans up the key of a key prefix

Shulou Source: shulou.com Published: 2022-06-02 19:20:02 09月15日 Update

Redis cleans up the key of a prefix

For example, a research and development of online code, resulting in a certain prefix key dirty data (for example, the key prefix name is key_), we need to quickly clean up these problems key.

There are two common methods:

1. Dump produces a full amount of data, then finds out the key that meets the conditions, and deletes it [recommended]

2. Scan the redis with scan, fish out all the key, and then delete the qualified key

We use the first method here, as follows:

1. On the slave node, bgsave commands dump to output a full amount of data

Bgsave, the generated file name is similar to dump-6379.rdb

2. Install the rdb parsing tool

The next line of command pip install rdbtools python-lzf # python2.7 can complete the installation of rdb-c memory dump-6379.rdb > memory.csv # use this command to analyze rdb

3. Filter out the qualified key

Awk-F',''{print $3 $NF} 'memory.csv > keys.txt # filter out the name and expiration time of the key egrep key_ keys.txt > / root/key_.txt # filter out the key of the key_ prefix cat / root/key_.txt | sort-k 2-r > / root/sort_keys # sort the key in reverse order by date egrep 2019-09-10 / root/sort_keys > / root/match_keys # Note: I am here to deal with urgently Only 2019-09-10 expired key is filtered (this is the latest data and the most frequently visited key in business at present. Awk'{print $1}'/ root/match_keys > / root/filter_keys # redirects the final key to a file mkdir / root/test/split-2000 / root/filter_keys / root/test/ # splits the filter_keys into multiple files according to each 2k line for subsequent parallel processing

Then, let's write a script and batch process:

The vim / root/batch.sh content is as follows:

For i in `ls / root/test/ `; doecho "while read line;doecho\" del\ $line\ "| redis-cli-h 127.0.0.1-p 6379 done

< /root/test/${i}" >

/ root/run_$ {I} .shchmod + x / root/run_$ {I} .sh done

This generates a bunch of scripts.

Let's write a script and execute it in batch.

#! / bin/bashfor I in `ls run* .sh`; do nohup sh $I > / dev/null & done

Tags: Processing prefixes data files commands conditions scripts names methods generation line business that is code reverse order content multiple tools commonly used file names Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Shulou Tech Info Shulou Technology NVidia