<> Problems encountered :
During the development process , Some rules will be deleted in batch key, for example login_logID(ID Is a variable ), It needs to be deleted now "login_log*" This kind of data , however redis There is only batch query key Command of value keys, But there is no command to batch delete a class .
<> terms of settlement :
Query first , In delete , use xargs Transmission parameter (xargs You can import pipes or standards (stdin) Data conversion to command line parameters ), Execute query statement first , After the query key value , original del To delete the parameter of .
redis-cli KEYS key* ( Search criteria ) | xargs redis-cli del
=>[ Influence quantity of returned results after execution ]:(integer) 10[ quantity 10 individual ]
Do an experiment , First create three of the same type key value
127.0.0.1:6379> set test1 1 OK 127.0.0.1:6379> set test2 2 OK 127.0.0.1:6379>
set test3 3 OK
query keys
127.0.0.1:6379> keys test* 1) "test3" 2) "test2" 3) "test1"
sign out redis, Execute delete command locally
[root@localhost redis]# redis-cli -a password -n 0( database ) keys "test*" |xargs
redis-cli -a password -n 0( database ) del Warning: Using a password with '-a' or '-u'
option on the command line interface may not be safe. Warning: Using a password
with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3 ( Return rows )
Principle analysis :
This order is passed first redis Client executed keys command , Fuzzy search out all key, adopt xargs command , The previous query key As the back redis of del Command input
Equivalent to the implementation redis-cli del test1 test2 test3
be careful : You need to take it with you when executing here redis condition ,-a Yes, enter the password ,-n Is the specified database , If redis Not local or other changes need to be added -h
redis Server ip,-p port
for example :redis-cli -h 127.0.0.1(IP address ) -p 6379 ( Port number ) -a password -n 1( Write as many data as you want in the database ) KEYS
key* ( Search criteria ) | xargs redis-cli (-h (IP address ) -p 6379 ( Port number ) -a password -n 1 ) del
<> Complementary knowledge :
redis Deletion in
1.Redis DEL Command to delete an existing key , Nonexistent key Will be ignored .
for example :
127.0.0.1:6379> set hello world OK 127.0.0.1:6379> del hello (integer) 1
however del Only one or more can be deleted , Cannot delete in batch , It is not applicable when the amount of data to be deleted is too large
2. Empty the whole Redis Server data :flushall
3. Clear all in the current library key:flushdb
Technology
Daily Recommendation