在我的Redis DB中,我有一些前缀:<numeric_id>哈希值。
有时我想把它们都原子地清除掉。如何在不使用分布式锁定机制的情况下做到这一点呢?
在我的Redis DB中,我有一些前缀:<numeric_id>哈希值。
有时我想把它们都原子地清除掉。如何在不使用分布式锁定机制的情况下做到这一点呢?
当前回答
我支持所有与有一些工具或执行Lua表达式相关的答案。
我还有一个选择:
在我们的生产和预生产数据库中,有数千个键。时不时地,我们需要删除一些键(通过一些掩码),修改一些标准等。当然,没有办法从CLI手动完成,特别是使用分片(每个物理分区中有512个逻辑dbs)。
为此,我编写了一个java客户端工具来完成所有这些工作。在删除键的情况下,实用程序可以非常简单,只有一个类:
public class DataCleaner {
public static void main(String args[]) {
String keyPattern = args[0];
String host = args[1];
int port = Integer.valueOf(args[2]);
int dbIndex = Integer.valueOf(args[3]);
Jedis jedis = new Jedis(host, port);
int deletedKeysNumber = 0;
if(dbIndex >= 0){
deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, dbIndex);
} else {
int dbSize = Integer.valueOf(jedis.configGet("databases").get(1));
for(int i = 0; i < dbSize; i++){
deletedKeysNumber += deleteDataFromDB(jedis, keyPattern, i);
}
}
if(deletedKeysNumber == 0) {
System.out.println("There is no keys with key pattern: " + keyPattern + " was found in database with host: " + host);
}
}
private static int deleteDataFromDB(Jedis jedis, String keyPattern, int dbIndex) {
jedis.select(dbIndex);
Set<String> keys = jedis.keys(keyPattern);
for(String key : keys){
jedis.del(key);
System.out.println("The key: " + key + " has been deleted from database index: " + dbIndex);
}
return keys.size();
}
}
其他回答
如果你使用windows环境,请遵循以下步骤,它一定会工作:
Download GOW from here - https://github.com/bmatzelle/gow/wiki (because xargs command doesn't works in windows) Download redis-cli for Windows (detailed explanation is here - https://medium.com/@binary10111010/redis-cli-installation-on-windows-684fb6b6ac6b) Run cmd and open directory where redis-cli stores (example: D:\Redis\Redis-x64-3.2.100) if you want to delete all keys which start with "Global:ProviderInfo" execute this query (it's require to change bold parameters (host, port, password, key) and write yours, because of this is only example): redis-cli -h redis.test.com -p 6379 -a redispassword --raw keys "Global:ProviderInfo*" | xargs redis-cli -h redis.test.com -p 6379 -a redispassword del
您还可以使用该命令删除密钥
假设你的redis中有很多类型的键,比如-
“xyz_category_fpc_12” “xyz_category_fpc_245” “xyz_category_fpc_321” “xyz_product_fpc_876” “xyz_product_fpc_302” “xyz_product_fpc_01232”
Ex- 'xyz_category_fpc'这里xyz是一个站点名称,这些键与电子商务网站的产品和类别相关,由FPC生成。
如果您像下面那样使用此命令-
redis-cli --scan --pattern 'key*' | xargs redis-cli del
OR
redis-cli --scan --pattern 'xyz_category_fpc*' | xargs redis-cli del
它删除所有的键,如“xyz_category_fpc”(删除1、2和3个键)。若要删除其他4,5和6数字键,请使用上述命令中的'xyz_product_fpc'。
如果你想删除所有在Redis,然后按照这些命令-
redis-cli:
FLUSHDB -从连接的当前数据库中删除数据。 FLUSHALL—从所有数据库中删除数据。
例如:-在你的壳:
redis-cli flushall
redis-cli flushdb
我在redis 3.2.8中使用以下命令
redis-cli KEYS *YOUR_KEY_PREFIX* | xargs redis-cli DEL
您可以从这里获得更多有关键模式搜索的帮助:- https://redis.io/commands/keys。使用您方便的全局样式模式,如*YOUR_KEY_PREFIX*或YOUR_KEY_PREFIX??或者其他的。
如果你已经集成了Redis PHP库,下面的函数将帮助你。
flushRedisMultipleHashKeyUsingPattern("*YOUR_KEY_PATTERN*"); //function call
function flushRedisMultipleHashKeyUsingPattern($pattern='')
{
if($pattern==''){
return true;
}
$redisObj = $this->redis;
$getHashes = $redisObj->keys($pattern);
if(!empty($getHashes)){
$response = call_user_func_array(array(&$redisObj, 'del'), $getHashes); //setting all keys as parameter of "del" function. Using this we can achieve $redisObj->del("key1","key2);
}
}
谢谢!
请使用此命令并尝试:
redis-cli --raw keys "$PATTERN" | xargs redis-cli del
如果你在键的名称中有空格,你可以在bash中使用:
redis-cli keys "pattern: *" | xargs -L1 -I '$' echo '"$"' | xargs redis-cli del