@Override public Boolean set(byte[] key, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); try { return Converters.stringToBoolean(connection.getCluster().set(key, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public byte[] get(byte[] key) { Assert.notNull(key, "Key must not be null!"); try { return connection.getCluster().get(key); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long del(byte[] key) { return jedisCluster.del(key); }
@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 Boolean set(byte[] key, byte[] value, Expiration expiration, SetOption option) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); Assert.notNull(expiration, "Expiration must not be null!"); Assert.notNull(option, "Option must not be null!"); return set(key, value); } else { return setNX(key, value); return pSetEx(key, expiration.getExpirationTime(), value); } else { return setEx(key, expiration.getExpirationTime(), value); SetParams params = JedisConverters.toSetCommandNxXxArgument(option); JedisConverters.toSetCommandExPxArgument(expiration, params); return Converters.stringToBoolean(connection.getCluster().set(key, value, params)); } catch (Exception ex) { throw convertJedisAccessException(ex);
@Override public Boolean hSet(byte[] key, byte[] field, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(field, "Field must not be null!"); Assert.notNull(value, "Value must not be null!"); try { return JedisConverters.toBoolean(connection.getCluster().hset(key, field, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@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 byte[] hGet(byte[] key, byte[] field) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(field, "Field must not be null!"); try { return connection.getCluster().hget(key, field); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean setEx(byte[] key, long seconds, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); if (seconds > Integer.MAX_VALUE) { throw new IllegalArgumentException("Seconds have cannot exceed Integer.MAX_VALUE!"); } try { return Converters.stringToBoolean(connection.getCluster().setex(key, Long.valueOf(seconds).intValue(), value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long hDel(byte[] key, byte[]... fields) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(fields, "Fields must not be null!"); try { return connection.getCluster().hdel(key, fields); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
/** * Creates {@link JedisCluster} for given {@link RedisClusterConfiguration} and {@link GenericObjectPoolConfig}. * * @param clusterConfig must not be {@literal null}. * @param poolConfig can be {@literal null}. * @return the actual {@link JedisCluster}. * @since 1.7 */ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig, GenericObjectPoolConfig poolConfig) { Assert.notNull(clusterConfig, "Cluster configuration must not be null!"); Set<HostAndPort> hostAndPort = new HashSet<>(); for (RedisNode node : clusterConfig.getClusterNodes()) { hostAndPort.add(new HostAndPort(node.getHost(), node.getPort())); } int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects() : 5; int connectTimeout = getConnectTimeout(); int readTimeout = getReadTimeout(); return StringUtils.hasText(getPassword()) ? new JedisCluster(hostAndPort, connectTimeout, readTimeout, redirects, getPassword(), poolConfig) : new JedisCluster(hostAndPort, connectTimeout, readTimeout, redirects, poolConfig); }
/** * @return * @since 1.7 */ protected JedisCluster createCluster() { Set<HostAndPort> hostAndPorts = redisServers .stream() .map(redisNode -> new HostAndPort(redisNode.host, redisNode.port)) .collect(Collectors.toSet()); if (StringUtils.isNotEmpty(getPassword())) { throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!"); } int redirects = 5; return new JedisCluster(hostAndPorts, timeout, redirects, poolConfig); }
/** * {@inheritDoc} */ @Override protected void updateStatesToRedis(Map<String, String> keyValues) { RedisDataTypeDescription description = this.options.dataTypeDescription; switch (description.getDataType()) { case STRING: for (Map.Entry<String, String> kvEntry : keyValues.entrySet()) { if (this.options.expireIntervalSec > 0) { jedisCluster.setex(kvEntry.getKey(), this.options.expireIntervalSec, kvEntry.getValue()); } else { jedisCluster.set(kvEntry.getKey(), kvEntry.getValue()); } } break; case HASH: jedisCluster.hmset(description.getAdditionalKey(), keyValues); if (this.options.expireIntervalSec > 0) { jedisCluster.expire(description.getAdditionalKey(), this.options.expireIntervalSec); } break; default: throw new IllegalArgumentException("Cannot process such data type: " + description.getDataType()); } }
@Override public String set(final String key, final String value) { return jedisCluster.set(key, value); }
@Override public void caching(String key, T cache) { byte[] cacheKey = key.getBytes(Consts.UTF_8); byte[] value = SerializationUtils.serialize(cache); if (cache.getExpires() > 0) { jedisCluster.setex(cacheKey, (int) (cache.getExpires() - CUTMS) / 1000, value); } else { jedisCluster.set(cacheKey, value); } jedisCluster.sadd(ALLKEY, key); }
@Override public String get(final String key) { return jedisCluster.get(key); }
@Override public void close() throws IOException { jedisCluster.close(); } }