Add Redis 2.6 dump and restore commands to RedisTemplate
DATAREDIS-182
This commit is contained in:
@@ -99,6 +99,10 @@ public interface RedisOperations<K, V> {
|
||||
|
||||
Boolean move(K key, int dbIndex);
|
||||
|
||||
byte[] dump(K key);
|
||||
|
||||
void restore(K key, byte[] value, long timeToLive, TimeUnit unit);
|
||||
|
||||
Long getExpire(K key);
|
||||
|
||||
void watch(K keys);
|
||||
|
||||
@@ -617,6 +617,50 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
|
||||
}, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the Redis dump command and returns the results. Redis uses a
|
||||
* non-standard serialization mechanism and includes checksum information,
|
||||
* thus the raw bytes are returned as opposed to deserializing with
|
||||
* valueSerializer. Use the return value of dump as the value argument to restore
|
||||
*
|
||||
* @param key The key to dump
|
||||
* @return results The results of the dump operation
|
||||
*/
|
||||
public byte[] dump(K key) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
|
||||
return execute(new RedisCallback<byte[]>() {
|
||||
public byte[] doInRedis(RedisConnection connection) {
|
||||
return connection.dump(rawKey);
|
||||
}
|
||||
}, 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
|
||||
* @throws RedisSystemException if the key you are attempting to restore already
|
||||
* exists.
|
||||
*
|
||||
*/
|
||||
public void restore(K key, final byte[] value, long timeToLive, TimeUnit unit) {
|
||||
final byte[] rawKey = rawKey(key);
|
||||
final long rawTimeout = TimeoutUtils.toMillis(timeToLive, unit);
|
||||
|
||||
execute(new RedisCallback<Object>() {
|
||||
public Boolean doInRedis(RedisConnection connection) {
|
||||
connection.restore(rawKey, rawTimeout, value);
|
||||
return null;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
public void multi() {
|
||||
execute(new RedisCallback<Object>() {
|
||||
|
||||
@@ -38,12 +38,33 @@ abstract public class TimeoutUtils {
|
||||
* @return The converted timeout
|
||||
*/
|
||||
public static long toSeconds(long timeout, TimeUnit unit) {
|
||||
long seconds = unit.toSeconds(timeout);
|
||||
return roundUpIfNecessary(timeout, unit.toSeconds(timeout));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
|
||||
private static long roundUpIfNecessary(long timeout, long convertedTimeout) {
|
||||
// A 0 timeout blocks some Redis ops indefinitely, round up if that's
|
||||
// not the intention
|
||||
if (timeout > 0 && seconds == 0) {
|
||||
seconds = 1;
|
||||
if (timeout > 0 && convertedTimeout == 0) {
|
||||
return 1;
|
||||
}
|
||||
return seconds;
|
||||
return convertedTimeout;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user