@Override public Boolean expire(K key, final long timeout, final TimeUnit unit) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeout, unit); return execute(connection -> { try { return connection.pExpire(rawKey, rawTimeout); } catch (Exception e) { // Driver may not support pExpire or we may be running on Redis 2.4 return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit)); } }, true); }
/** * Set the {@code value} and expiration {@code timeout} for {@code key}. * * @param key must not be {@literal null}. * @param value must not be {@literal null}. * @param timeout must not be {@literal null}. * @throws IllegalArgumentException if either {@code key}, {@code value} or {@code timeout} is not present. * @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a> * @since 2.1 */ default void set(K key, V value, Duration timeout) { Assert.notNull(timeout, "Timeout must not be null!"); if (TimeoutUtils.hasMillis(timeout)) { set(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS); } else { set(key, value, timeout.getSeconds(), TimeUnit.SECONDS); } }
/** * Converts the given timeout to seconds. * <p> * Since a 0 timeout blocks some Redis ops indefinitely, this method will return 1 if the original value is greater * than 0 but is truncated to 0 on conversion. * * @param timeout The timeout to convert * @param unit The timeout's unit * @return The converted timeout */ public static long toSeconds(long timeout, TimeUnit unit) { return roundUpIfNecessary(timeout, unit.toSeconds(timeout)); }
@Override public V rightPop(K key, long timeout, TimeUnit unit) { int tm = (int) TimeoutUtils.toSeconds(timeout, unit); return execute(new ValueDeserializingRedisCallback(key) { @Override protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { List<byte[]> bRPop = connection.bRPop(tm, rawKey); return (CollectionUtils.isEmpty(bRPop) ? null : bRPop.get(1)); } }, true); }
/** * Executes the Redis restore command. The value passed in should be the exact serialized data returned from * {@link #dump(Object)}, since Redis uses a non-standard serialization mechanism. * * @param key The key to restore * @param value The value to restore, as returned by {@link #dump(Object)} * @param timeToLive An expiration for the restored key, or 0 for no expiration * @param unit The time unit for timeToLive * @param replace use {@literal true} to replace a potentially existing value instead of erroring. * @throws RedisSystemException if the key you are attempting to restore already exists and {@code replace} is set to * {@literal false}. */ @Override public void restore(K key, final byte[] value, long timeToLive, TimeUnit unit, boolean replace) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeToLive, unit); execute(connection -> { connection.restore(rawKey, rawTimeout, value, replace); return null; }, true); }
@Override public V leftPop(K key, long timeout, TimeUnit unit) { int tm = (int) TimeoutUtils.toSeconds(timeout, unit); return execute(new ValueDeserializingRedisCallback(key) { @Override protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { List<byte[]> lPop = connection.bLPop(tm, rawKey); return (CollectionUtils.isEmpty(lPop) ? null : lPop.get(1)); } }, true); }
public void putRedis( byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, long timeout,TimeUnit unit) { final byte[] paramArrayOfByte1Tmp = paramArrayOfByte1; final byte[] paramArrayOfByte2Tmp = paramArrayOfByte2; final long timeOutTmp = timeout; final TimeUnit unitTmp = unit; final long rawTimeout = TimeoutUtils.toMillis(timeout, unit); stringRedisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection redisConnection) throws DataAccessException { redisConnection.set(paramArrayOfByte1Tmp,paramArrayOfByte2Tmp); stringRedisTemplate.execute(new RedisCallback() { public Boolean doInRedis(RedisConnection connection) { try { return connection.pExpire(paramArrayOfByte1Tmp, rawTimeout); } catch (Exception var3) { return connection.expire(paramArrayOfByte1Tmp, TimeoutUtils.toSeconds(timeOutTmp, unitTmp)); } } },true); return null; } },true); }
@Override public Boolean expire(K key, final long timeout, final TimeUnit unit) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeout, unit); return execute(connection -> { try { return connection.pExpire(rawKey, rawTimeout); } catch (Exception e) { // Driver may not support pExpire or we may be running on Redis 2.4 return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit)); } }, true); }
/** * Set the bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is present. * * @param value must not be {@literal null}. * @param timeout must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. * @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present. * @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a> * @since 2.1 */ @Nullable default Boolean setIfPresent(V value, Duration timeout) { Assert.notNull(timeout, "Timeout must not be null!"); if (TimeoutUtils.hasMillis(timeout)) { return setIfPresent(value, timeout.toMillis(), TimeUnit.MILLISECONDS); } return setIfPresent(value, timeout.getSeconds(), TimeUnit.SECONDS); }
@Override public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) { int tm = (int) TimeoutUtils.toSeconds(timeout, unit); byte[] rawDestKey = rawKey(destinationKey); return execute(new ValueDeserializingRedisCallback(sourceKey) { @Override protected byte[] inRedis(byte[] rawSourceKey, RedisConnection connection) { return connection.bRPopLPush(tm, rawSourceKey, rawDestKey); } }, true); }
/** * Executes the Redis restore command. The value passed in should be the exact serialized data returned from * {@link #dump(Object)}, since Redis uses a non-standard serialization mechanism. * * @param key The key to restore * @param value The value to restore, as returned by {@link #dump(Object)} * @param timeToLive An expiration for the restored key, or 0 for no expiration * @param unit The time unit for timeToLive * @param replace use {@literal true} to replace a potentially existing value instead of erroring. * @throws RedisSystemException if the key you are attempting to restore already exists and {@code replace} is set to * {@literal false}. */ @Override public void restore(K key, final byte[] value, long timeToLive, TimeUnit unit, boolean replace) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeToLive, unit); execute(connection -> { connection.restore(rawKey, rawTimeout, value, replace); return null; }, true); }
/** * Converts the given timeout to milliseconds. * <p> * Since a 0 timeout blocks some Redis ops indefinitely, this method will return 1 if the original value is greater * than 0 but is truncated to 0 on conversion. * * @param timeout The timeout to convert * @param unit The timeout's unit * @return The converted timeout */ public static long toMillis(long timeout, TimeUnit unit) { return roundUpIfNecessary(timeout, unit.toMillis(timeout)); }
@Override public Boolean expire(K key, final long timeout, final TimeUnit unit) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeout, unit); return execute(connection -> { try { return connection.pExpire(rawKey, rawTimeout); } catch (Exception e) { // Driver may not support pExpire or we may be running on Redis 2.4 return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit)); } }, true); }
/** * Set the {@code value} and expiration {@code timeout} for the bound key. * * @param value must not be {@literal null}. * @param timeout must not be {@literal null}. * @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present. * @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a> * @since 2.1 */ default void set(V value, Duration timeout) { Assert.notNull(timeout, "Timeout must not be null!"); if (TimeoutUtils.hasMillis(timeout)) { set(value, timeout.toMillis(), TimeUnit.MILLISECONDS); } else { set(value, timeout.getSeconds(), TimeUnit.SECONDS); } }
public void potentiallyUsePsetEx(RedisConnection connection) { if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) { connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue); } }
/** * Executes the Redis restore command. The value passed in should be the exact serialized data returned from * {@link #dump(Object)}, since Redis uses a non-standard serialization mechanism. * * @param key The key to restore * @param value The value to restore, as returned by {@link #dump(Object)} * @param timeToLive An expiration for the restored key, or 0 for no expiration * @param unit The time unit for timeToLive * @param replace use {@literal true} to replace a potentially existing value instead of erroring. * @throws RedisSystemException if the key you are attempting to restore already exists and {@code replace} is set to * {@literal false}. */ @Override public void restore(K key, final byte[] value, long timeToLive, TimeUnit unit, boolean replace) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeToLive, unit); execute(connection -> { connection.restore(rawKey, rawTimeout, value, replace); return null; }, true); }
/** * Converts the given timeout to seconds. * <p> * Since a 0 timeout blocks some Redis ops indefinitely, this method will return 1 if the original value is greater * than 0 but is truncated to 0 on conversion. * * @param timeout The timeout to convert * @param unit The timeout's unit * @return The converted timeout */ public static long toSeconds(long timeout, TimeUnit unit) { return roundUpIfNecessary(timeout, unit.toSeconds(timeout)); }
/** * Set bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is absent. * * @param value must not be {@literal null}. * @param timeout must not be {@literal null}. * @return {@literal null} when used in pipeline / transaction. * @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present. * @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a> * @since 2.1 */ @Nullable default Boolean setIfAbsent(V value, Duration timeout) { Assert.notNull(timeout, "Timeout must not be null!"); if (TimeoutUtils.hasMillis(timeout)) { return setIfAbsent(value, timeout.toMillis(), TimeUnit.MILLISECONDS); } return setIfAbsent(value, timeout.getSeconds(), TimeUnit.SECONDS); }
@Override public V rightPop(K key, long timeout, TimeUnit unit) { int tm = (int) TimeoutUtils.toSeconds(timeout, unit); return execute(new ValueDeserializingRedisCallback(key) { @Override protected byte[] inRedis(byte[] rawKey, RedisConnection connection) { List<byte[]> bRPop = connection.bRPop(tm, rawKey); return (CollectionUtils.isEmpty(bRPop) ? null : bRPop.get(1)); } }, true); }
/** * Converts the given timeout to milliseconds. * <p> * Since a 0 timeout blocks some Redis ops indefinitely, this method will return 1 if the original value is greater * than 0 but is truncated to 0 on conversion. * * @param timeout The timeout to convert * @param unit The timeout's unit * @return The converted timeout */ public static long toMillis(long timeout, TimeUnit unit) { return roundUpIfNecessary(timeout, unit.toMillis(timeout)); }