Refine search
private JedisClusterKeyCommands doGetKeyCommands() { return new JedisClusterKeyCommands(this); }
@Override public Long ttl(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { return connection.getCluster().ttl(key); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public void rename(byte[] sourceKey, byte[] targetKey) { Assert.notNull(sourceKey, "Source key must not be null!"); Assert.notNull(targetKey, "Target key must not be null!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(sourceKey, targetKey)) { try { connection.getCluster().rename(sourceKey, targetKey); return; } catch (Exception ex) { throw convertJedisAccessException(ex); } } byte[] value = dump(sourceKey); if (value != null && value.length > 0) { restore(targetKey, 0, value); del(sourceKey); } }
@Override public Boolean renameNX(byte[] sourceKey, byte[] targetKey) { Assert.notNull(sourceKey, "Source key must not be null!"); Assert.notNull(targetKey, "Target key must not be null!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(sourceKey, targetKey)) { try { return JedisConverters.toBoolean(connection.getCluster().renamenx(sourceKey, targetKey)); } catch (Exception ex) { throw convertJedisAccessException(ex); } } byte[] value = dump(sourceKey); if (value != null && value.length > 0 && !exists(targetKey)) { restore(targetKey, 0, value); del(sourceKey); return Boolean.TRUE; } return Boolean.FALSE; }
@Override public Long sort(byte[] key, SortParameters params, byte[] storeKey) { Assert.notNull(key, "Key must not be null!"); List<byte[]> sorted = sort(key, params); if (!CollectionUtils.isEmpty(sorted)) { byte[][] arr = new byte[sorted.size()][]; switch (type(key)) { case SET: connection.setCommands().sAdd(storeKey, sorted.toArray(arr)); return 1L; case LIST: connection.listCommands().lPush(storeKey, sorted.toArray(arr)); return 1L; default: throw new IllegalArgumentException("sort and store is only supported for SET and LIST"); } } return 0L; }
@Override public Long del(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { try { return connection.getCluster().del(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } return (long) connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<Long>) (client, key) -> client.del(key), Arrays.asList(keys)) .resultsAsList().size(); }
@Nullable @Override public Long exists(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { try { return connection.getCluster().exists(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } return connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<Boolean>) BinaryJedis::exists, Arrays.asList(keys)) .resultsAsList().stream().mapToLong(val -> ObjectUtils.nullSafeEquals(val, Boolean.TRUE) ? 1 : 0).sum(); }
@Override public Boolean expire(byte[] key, long seconds) { Assert.notNull(key, "Key must not be null!"); if (seconds > Integer.MAX_VALUE) { throw new UnsupportedOperationException("Jedis does not support seconds exceeding Integer.MAX_VALUE."); } try { return JedisConverters.toBoolean(connection.getCluster().expire(key, Long.valueOf(seconds).intValue())); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean expireAt(byte[] key, long unixTime) { Assert.notNull(key, "Key must not be null!"); try { return JedisConverters.toBoolean(connection.getCluster().expireAt(key, unixTime)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean pExpire(byte[] key, long millis) { Assert.notNull(key, "Key must not be null!"); try { return JedisConverters.toBoolean(connection.getCluster().pexpire(key, millis)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public DataType type(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { return JedisConverters.toDataType(connection.getCluster().type(key)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public List<byte[]> sort(byte[] key, SortParameters params) { Assert.notNull(key, "Key must not be null!"); try { return connection.getCluster().sort(key, JedisConverters.toSortingParams(params)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean pExpireAt(byte[] key, long unixTimeInMillis) { Assert.notNull(key, "Key must not be null!"); try { return JedisConverters.toBoolean(connection.getCluster().pexpireAt(key, unixTimeInMillis)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean persist(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { return JedisConverters.toBoolean(connection.getCluster().persist(key)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public byte[] randomKey(RedisClusterNode node) { return doGetKeyCommands().randomKey(node); }
@Override public Set<byte[]> keys(RedisClusterNode node, byte[] pattern) { return doGetKeyCommands().keys(node, pattern); }
@Override public Cursor<byte[]> scan(RedisClusterNode node, ScanOptions options) { return doGetKeyCommands().scan(node, options); }
@Override public byte[] randomKey() { List<RedisClusterNode> nodes = new ArrayList<>( connection.getTopologyProvider().getTopology().getActiveMasterNodes()); Set<RedisNode> inspectedNodes = new HashSet<>(nodes.size()); do { RedisClusterNode node = nodes.get(ThreadLocalRandom.current().nextInt(nodes.size())); while (inspectedNodes.contains(node)) { node = nodes.get(ThreadLocalRandom.current().nextInt(nodes.size())); } inspectedNodes.add(node); byte[] key = randomKey(node); if (key != null && key.length > 0) { return key; } } while (nodes.size() != inspectedNodes.size()); return null; }
@Override public Long del(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { try { return connection.getCluster().del(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } return (long) connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<Long>) (client, key) -> client.del(key), Arrays.asList(keys)) .resultsAsList().size(); }
@Nullable @Override public Long exists(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { try { return connection.getCluster().exists(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } return connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<Boolean>) BinaryJedis::exists, Arrays.asList(keys)) .resultsAsList().stream().mapToLong(val -> ObjectUtils.nullSafeEquals(val, Boolean.TRUE) ? 1 : 0).sum(); }