通常,我得到密钥集,然后使用外观删除每个键/值对.
是否可以通过模式删除所有键?
即:
Del sample_pattern:*
iCodeLikeImD.. 18
看来,对于Jedis来说,"按模式删除"基本上是获取特定模式的所有键然后循环遍历它.
即
Setkeys = jedis.keys(pattern); for (String key : keys) { jedis.del(key); }
扫描更好.但是你可以通过`Set
小智.. 11
不建议使用KEYS,因为它在生产中使用效率低下.请参阅https://redis.io/commands/keys.相反,最好使用SCAN.另外,比重复调用jedis.del()更有效的调用是对jedis进行一次调用以删除匹配的键,传入要删除的键数组.下面介绍一种更有效的解决方案:
SetmatchingKeys = new HashSet<>(); ScanParams params = new ScanParams(); params.match("sample_pattern:*"); try(Jedis jedis = jedisPoolFactory.getPool().getResource()) { String nextCursor = "0"; do { ScanResult scanResult = jedis.scan(nextCursor, params); List 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()])); }
小智.. 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); } } }
然后打电话:
deleteKeys("temp:keys:*");
这保证了一个服务器端调用,多个删除操作.