Redis/Jedis - Delete by pattern?

2019-03-19 08:00发布

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:

Del sample_pattern:*

6条回答
Evening l夕情丶
2楼-- · 2019-03-19 08:17

You can do it with bash:

$ redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
查看更多
Root(大扎)
3楼-- · 2019-03-19 08:18

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:

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()]));
}
查看更多
Ridiculous、
4楼-- · 2019-03-19 08:22

You can do it with the Redisson in one line:

redisson.getKeys().deleteByPattern(pattern)
查看更多
爷的心禁止访问
5楼-- · 2019-03-19 08:23

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

ie

Set<String> keys = jedis.keys(pattern);
for (String key : keys) {
    jedis.del(key);
} 
查看更多
可以哭但决不认输i
6楼-- · 2019-03-19 08:23

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

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.

查看更多
三岁会撩人
7楼-- · 2019-03-19 08:27

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

String keyPattern = "sample_pattern:*";
Set<String> keys = jedis.keys(keyPattern);
for(String key:keys){
jedis.del(key);
}
查看更多
登录 后发表回答