@Override public List<byte[]> bRPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Key must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); return connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.brpop(timeout, key), Arrays.asList(keys)) .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList()); }
Assert.notNull(args, "Args must not be null!"); return clusterCommandExecutor.executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<T>) (client, key) -> { return JedisClientUtils.execute(command, new byte[][] { key }, args.toArray(new byte[args.size()][]), () -> client);
@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 List<byte[]> mGet(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return connection.getCluster().mget(keys); } return connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<byte[]>) BinaryJedis::get, Arrays.asList(keys)) .resultsAsListSortBy(keys); }
@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(); }
@Override public List<byte[]> bRPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.bRPop(timeout, keys); } List<KeyValue<byte[], byte[]>> resultList = connection.getClusterCommandExecutor().executeMultiKeyCommand( (LettuceMultiKeyClusterCommandCallback<KeyValue<byte[], byte[]>>) (client, key) -> client.brpop(timeout, key), Arrays.asList(keys)).resultsAsList(); for (KeyValue<byte[], byte[]> kv : resultList) { if (kv != null) { return LettuceConverters.toBytesList(kv); } } return Collections.emptyList(); }
@Override public List<byte[]> bLPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.bLPop(timeout, keys); } List<KeyValue<byte[], byte[]>> resultList = connection.getClusterCommandExecutor().executeMultiKeyCommand( (LettuceMultiKeyClusterCommandCallback<KeyValue<byte[], byte[]>>) (client, key) -> client.blpop(timeout, key), Arrays.asList(keys)).resultsAsList(); for (KeyValue<byte[], byte[]> kv : resultList) { if (kv != null) { return LettuceConverters.toBytesList(kv); } } return Collections.emptyList(); }
@Override public List<byte[]> bLPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Key must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { try { return connection.getCluster().blpop(timeout, keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } return connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.blpop(timeout, key), Arrays.asList(keys)) .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList()); }
@Override public Set<byte[]> sDiff(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sDiff(keys); } byte[] source = keys[0]; byte[][] others = Arrays.copyOfRange(keys, 1, keys.length); ByteArraySet values = new ByteArraySet(sMembers(source)); Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(others)) .resultsAsList(); if (values.isEmpty()) { return Collections.emptySet(); } for (Set<byte[]> toSubstract : nodeResult) { values.removeAll(toSubstract); } return values.asRawSet(); }
@Override public Set<byte[]> sUnion(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sUnion(keys); } Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(keys)) .resultsAsList(); ByteArraySet result = new ByteArraySet(); for (Set<byte[]> entry : nodeResult) { result.addAll(entry); } if (result.isEmpty()) { return Collections.emptySet(); } return result.asRawSet(); }
@Override public Set<byte[]> sDiff(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().sdiff(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } byte[] source = keys[0]; byte[][] others = Arrays.copyOfRange(keys, 1, keys.length); ByteArraySet values = new ByteArraySet(sMembers(source)); Collection<Set<byte[]>> resultList = connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<Set<byte[]>>) (client, key) -> client.smembers(key), Arrays.asList(others)) .resultsAsList(); if (values.isEmpty()) { return Collections.emptySet(); } for (Set<byte[]> singleNodeValue : resultList) { values.removeAll(singleNodeValue); } return values.asRawSet(); }
@Override public Set<byte[]> sInter(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sInter(keys); } Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(keys)) .resultsAsList(); ByteArraySet result = null; for (Set<byte[]> entry : nodeResult) { ByteArraySet tmp = new ByteArraySet(entry); if (result == null) { result = tmp; } else { result.retainAll(tmp); if (result.isEmpty()) { break; } } } if (result == null || result.isEmpty()) { return Collections.emptySet(); } return result.asRawSet(); }
@Override public Set<byte[]> sUnion(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().sunion(keys); } catch (Exception ex) { throw convertJedisAccessException(ex); } } Collection<Set<byte[]>> resultList = connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<Set<byte[]>>) (client, key) -> client.smembers(key), Arrays.asList(keys)) .resultsAsList(); ByteArraySet result = new ByteArraySet(); for (Set<byte[]> entry : resultList) { result.addAll(entry); } if (result.isEmpty()) { return Collections.emptySet(); } return result.asRawSet(); }
.executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<Set<byte[]>>) (client, key) -> client.smembers(key), Arrays.asList(keys))
@Override public List<byte[]> bRPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Key must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); return connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.brpop(timeout, key), Arrays.asList(keys)) .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList()); }
@Override public List<byte[]> bRPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Key must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); return connection.getClusterCommandExecutor() .executeMultiKeyCommand( (JedisMultiKeyClusterCommandCallback<List<byte[]>>) (client, key) -> client.brpop(timeout, key), Arrays.asList(keys)) .getFirstNonNullNotEmptyOrDefault(Collections.<byte[]> emptyList()); }
@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 List<byte[]> mGet(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return connection.getCluster().mget(keys); } return connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<byte[]>) BinaryJedis::get, Arrays.asList(keys)) .resultsAsListSortBy(keys); }
@Override public List<byte[]> mGet(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return connection.getCluster().mget(keys); } return connection.getClusterCommandExecutor() .executeMultiKeyCommand((JedisMultiKeyClusterCommandCallback<byte[]>) BinaryJedis::get, Arrays.asList(keys)) .resultsAsListSortBy(keys); }
@Override public List<byte[]> bLPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.bLPop(timeout, keys); } List<KeyValue<byte[], byte[]>> resultList = connection.getClusterCommandExecutor().executeMultiKeyCommand( (LettuceMultiKeyClusterCommandCallback<KeyValue<byte[], byte[]>>) (client, key) -> client.blpop(timeout, key), Arrays.asList(keys)).resultsAsList(); for (KeyValue<byte[], byte[]> kv : resultList) { if (kv != null) { return LettuceConverters.toBytesList(kv); } } return Collections.emptyList(); }