iCo*_*unk 10 java del redis jedis
通常,我得到密钥集,然后使用外观删除每个键/值对.
是否可以通过模式删除所有键?
即:
Del sample_pattern:*
Run Code Online (Sandbox Code Playgroud)
iCo*_*unk 18
看来,对于Jedis来说,"按模式删除"基本上是获取特定模式的所有键然后循环遍历它.
即
Set<String> keys = jedis.keys(pattern);
for (String key : keys) {
jedis.del(key);
}
Run Code Online (Sandbox Code Playgroud)
小智 11
不建议使用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()]));
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Redisson在一行中完成此操作:
redisson.getKeys().deleteByPattern(pattern)
Run Code Online (Sandbox Code Playgroud)
小智 5
你应该尝试使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后打电话:
deleteKeys("temp:keys:*");
Run Code Online (Sandbox Code Playgroud)
这保证了一个服务器端调用,多个删除操作.
| 归档时间: |
|
| 查看次数: |
23645 次 |
| 最近记录: |