Java Redis/Jedis - 按模式删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21317501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:01:15  来源:igfitidea点击:

Redis/Jedis - Delete by pattern?

javaredisjedisdel

提问by iCodeLikeImDrunk

Normally, I get the key set then use a look to delete each key/value pair.

通常,我获取键集然后使用外观删除每个键/值对。

Is it possible to just delete all keys via pattern?

是否可以通过模式删除所有键?

ie:

IE:

Del sample_pattern:*

采纳答案by iCodeLikeImDrunk

It seems, for Jedis, to "delete by pattern" is basically getting all the keys of a specific pattern then loop through it.

对于 Jedis 来说,“按模式删除”似乎基本上是获取特定模式的所有键,然后循环遍历它。

ie

IE

Set<String> keys = jedis.keys(pattern);
for (String key : keys) {
    jedis.del(key);
} 

回答by Agis

You can do it with bash:

你可以用 bash 做到这一点:

$ redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL

回答by corindiano

You should try using eval. I'm no Lua expert, but this code works.

您应该尝试使用eval。我不是 Lua 专家,但此代码有效。

private static final String DELETE_SCRIPT_IN_LUA =
    "local keys = redis.call('keys', '%s')" +
    "  for i,k in ipairs(keys) do" +
    "    local res = redis.call('del', k)" +
    "  end";

public void deleteKeys(String pattern) {
  Jedis jedis = null;

  try {
    jedis = jedisPool.getResource();

    if (jedis == null) {
      throw new Exception("Unable to get jedis resource!");
    }

    jedis.eval(String.format(DELETE_SCRIPT_IN_LUA, pattern));  
  } catch (Exception exc) {
    if (exc instance of JedisConnectionException && jedis != null) {
      jedisPool.returnBrokenResource(jedis);
      jedis = null;
    }

    throw new RuntimeException("Unable to delete that pattern!");
  } finally {
    if (jedis != null) {
      jedisPool.returnResource(jedis);
    }
  }
}

And then call:

然后调用:

deleteKeys("temp:keys:*");

This guarantees a one server-side call, multiple delete operation.

这保证了一次服务器端调用,多次删除操作。

回答by Nikita Koksharov

You can do it with the Redissonin one line:

您可以在一行中使用Redisson完成此操作:

redisson.getKeys().deleteByPattern(pattern)

回答by VanThaoNguyen

Using java to delete, it's seem like this:

用java删除,好像是这样的:

String keyPattern = "sample_pattern:*";
Set<String> keys = jedis.keys(keyPattern);
for(String key:keys){
jedis.del(key);
}

回答by Aprille

KEYS is not recommended to use due to its inefficiencies when used in production. Please see https://redis.io/commands/keys. Instead, it is better to use SCAN. Additionally, a more efficient call than repeated calls to jedis.del() is to make one single call to jedis to remove the matching keys, passing in an array of keys to delete. A more efficient solution is presented below:

不建议使用 KEYS,因为它在生产中使用效率低下。请参阅https://redis.io/commands/keys。相反,最好使用 SCAN。此外,比重复调用 jedis.del() 更有效的调用是对 jedis 进行一次调用以删除匹配的键,传入要删除的键数组。下面给出了一个更有效的解决方案:

Set<String> matchingKeys = new HashSet<>();
ScanParams params = new ScanParams();
params.match("sample_pattern:*");

try(Jedis jedis = jedisPoolFactory.getPool().getResource()) {
    String nextCursor = "0";

    do {
        ScanResult<String> scanResult = jedis.scan(nextCursor, params);
        List<String> keys = scanResult.getResult();
        nextCursor = scanResult.getStringCursor();

        matchingKeys.addAll(keys);

    } while(!nextCursor.equals("0"));

    if (matchingKeys.size() == 0) {
      return;
    }

    jedis.del(matchingKeys.toArray(new String[matchingKeys.size()]));
}