DATAREDIS-526 - Allow using ttl and pttl with TimeUnit on connection-level.
We now support ttl and pttl accepting a TimeUnit on connection level to return the TTL in the requested time unit. By performing the conversion inside the connection we return correct results for plain, pipelining and transaction execution. Add also JavaDoc and @Override to ttl/pttl methods. Original pull request: #205.
This commit is contained in:
@@ -967,6 +967,22 @@ public abstract class AbstractConnectionIntegrationTests {
|
||||
verifyResults(Arrays.asList(new Object[] { -1L }));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void testTtlWithTimeUnit() {
|
||||
|
||||
connection.set("whatup", "yo");
|
||||
actual.add(connection.expire("whatup", 10));
|
||||
actual.add(connection.ttl("whatup", TimeUnit.MILLISECONDS));
|
||||
|
||||
List<Object> results = getResults();
|
||||
|
||||
assertTrue((Long) results.get(1) > TimeUnit.SECONDS.toMillis(5));
|
||||
assertTrue((Long) results.get(1) <= TimeUnit.SECONDS.toMillis(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testPTtlNoExpire() {
|
||||
@@ -978,25 +994,48 @@ public abstract class AbstractConnectionIntegrationTests {
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testPTtl() {
|
||||
|
||||
connection.set("whatup", "yo");
|
||||
actual.add(connection.pExpire("whatup", 9000l));
|
||||
actual.add(connection.pExpire("whatup", TimeUnit.SECONDS.toMillis(10)));
|
||||
actual.add(connection.pTtl("whatup"));
|
||||
|
||||
List<Object> results = getResults();
|
||||
|
||||
assertTrue((Long) results.get(1) > -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testPTtlWithTimeUnit() {
|
||||
|
||||
connection.set("whatup", "yo");
|
||||
actual.add(connection.pExpire("whatup", TimeUnit.MINUTES.toMillis(10)));
|
||||
actual.add(connection.pTtl("whatup", TimeUnit.SECONDS));
|
||||
|
||||
List<Object> results = getResults();
|
||||
|
||||
assertTrue((Long) results.get(1) > TimeUnit.MINUTES.toSeconds(9));
|
||||
assertTrue((Long) results.get(1) <= TimeUnit.MINUTES.toSeconds(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
@IfProfileValue(name = "redisVersion", value = "2.6+")
|
||||
public void testDumpAndRestore() {
|
||||
|
||||
connection.set("testing", "12");
|
||||
actual.add(connection.dump("testing".getBytes()));
|
||||
List<Object> results = getResults();
|
||||
initConnection();
|
||||
|
||||
actual.add(connection.del("testing"));
|
||||
actual.add((connection.get("testing")));
|
||||
connection.restore("testing".getBytes(), 0, (byte[]) results.get(results.size() - 1));
|
||||
actual.add(connection.get("testing"));
|
||||
verifyResults(Arrays.asList(new Object[] { 1l, null, "12" }));
|
||||
|
||||
verifyResults(Arrays.asList(new Object[] { 1L, null, "12" }));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.springframework.data.geo.Point;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface ClusterConnectionTests {
|
||||
|
||||
@@ -119,7 +120,7 @@ public interface ClusterConnectionTests {
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
void persistShoudRemoveTTL();
|
||||
void persistShouldRemoveTTL();
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
@@ -141,6 +142,11 @@ public interface ClusterConnectionTests {
|
||||
*/
|
||||
void ttlShouldReturnMinusTwoWhenKeyDoesNotExist();
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
void ttlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist();
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@@ -156,6 +162,11 @@ public interface ClusterConnectionTests {
|
||||
*/
|
||||
void pTtlShouldReturnMinusTwoWhenKeyDoesNotExist();
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
void pTtlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist();
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Ninad Divadkar
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class DefaultStringRedisConnectionPipelineTests extends DefaultStringRedisConnectionTests {
|
||||
|
||||
@@ -973,6 +974,15 @@ public class DefaultStringRedisConnectionPipelineTests extends DefaultStringRedi
|
||||
super.testTtl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Override
|
||||
public void testTtlWithTimeUnit() {
|
||||
doReturn(Arrays.asList(new Object[] { 5L })).when(nativeConnection).closePipeline();
|
||||
super.testTtlWithTimeUnit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeBytes() {
|
||||
doReturn(Arrays.asList(new Object[] { DataType.HASH })).when(nativeConnection).closePipeline();
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.data.redis.connection.RedisGeoCommands.DistanceUnit;
|
||||
* @author Jennifer Hickey
|
||||
* @author Christoph Strobl
|
||||
* @author Ninad Divadkar
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class DefaultStringRedisConnectionPipelineTxTests extends DefaultStringRedisConnectionTxTests {
|
||||
|
||||
@@ -1047,6 +1048,16 @@ public class DefaultStringRedisConnectionPipelineTxTests extends DefaultStringRe
|
||||
super.testTtl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Override
|
||||
public void testTtlWithTimeUnit() {
|
||||
|
||||
doReturn(Arrays.asList(new Object[] { Arrays.asList(new Object[] { 5L }) })).when(nativeConnection).closePipeline();
|
||||
super.testTtlWithTimeUnit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeBytes() {
|
||||
doReturn(Arrays.asList(new Object[] { Arrays.asList(new Object[] { DataType.HASH }) })).when(nativeConnection)
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -1193,6 +1194,18 @@ public class DefaultStringRedisConnectionTests {
|
||||
verifyResults(Arrays.asList(new Object[] { 5l }));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void testTtlWithTimeUnit() {
|
||||
|
||||
doReturn(5L).when(nativeConnection).ttl(fooBytes, TimeUnit.SECONDS);
|
||||
|
||||
actual.add(connection.ttl(foo, TimeUnit.SECONDS));
|
||||
verifyResults(Arrays.asList(new Object[] { 5L }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeBytes() {
|
||||
doReturn(DataType.HASH).when(nativeConnection).type(fooBytes);
|
||||
|
||||
@@ -959,6 +959,16 @@ public class DefaultStringRedisConnectionTxTests extends DefaultStringRedisConne
|
||||
super.testTtl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Override
|
||||
public void testTtlWithTimeUnit() {
|
||||
|
||||
doReturn(Arrays.asList(new Object[] { 5L })).when(nativeConnection).exec();
|
||||
super.testTtl();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeBytes() {
|
||||
doReturn(Arrays.asList(new Object[] { DataType.HASH })).when(nativeConnection).exec();
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -44,6 +45,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Thomas Darimont
|
||||
* @author David Liu
|
||||
* @author Ninad Divadkar
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class RedisConnectionUnitTests {
|
||||
|
||||
@@ -688,6 +690,10 @@ public class RedisConnectionUnitTests {
|
||||
return delegate.ttl(key);
|
||||
}
|
||||
|
||||
public Long ttl(byte[] key, TimeUnit timeUnit) {
|
||||
return delegate.pTtl(key, timeUnit);
|
||||
}
|
||||
|
||||
public List<byte[]> bLPop(int timeout, byte[]... keys) {
|
||||
return delegate.bLPop(timeout, keys);
|
||||
}
|
||||
@@ -708,6 +714,10 @@ public class RedisConnectionUnitTests {
|
||||
return delegate.pTtl(key);
|
||||
}
|
||||
|
||||
public Long pTtl(byte[] key, TimeUnit timeUnit) {
|
||||
return delegate.pTtl(key, timeUnit);
|
||||
}
|
||||
|
||||
public Cursor<byte[]> sScan(byte[] key, ScanOptions options) {
|
||||
return delegate.sScan(key, options);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -378,7 +379,7 @@ public class JedisClusterConnectionTests implements ClusterConnectionTests {
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@Test
|
||||
public void persistShoudRemoveTTL() {
|
||||
public void persistShouldRemoveTTL() {
|
||||
|
||||
nativeConnection.setex(KEY_1_BYTES, 10, VALUE_1_BYTES);
|
||||
|
||||
@@ -428,6 +429,14 @@ public class JedisClusterConnectionTests implements ClusterConnectionTests {
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void ttlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist() {
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES, TimeUnit.HOURS), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@@ -459,6 +468,14 @@ public class JedisClusterConnectionTests implements ClusterConnectionTests {
|
||||
assertThat(clusterConnection.pTtl(KEY_1_BYTES), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void pTtlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist() {
|
||||
assertThat(clusterConnection.pTtl(KEY_1_BYTES, TimeUnit.HOURS), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
|
||||
@@ -349,6 +349,14 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
|
||||
super.testPTtlNoExpire();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testPTtlWithTimeUnit() {
|
||||
super.testPTtlWithTimeUnit();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testDumpAndRestore() {
|
||||
super.testDumpAndRestore();
|
||||
|
||||
@@ -377,7 +377,7 @@ public class LettuceClusterConnectionTests implements ClusterConnectionTests {
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@Test
|
||||
public void persistShoudRemoveTTL() {
|
||||
public void persistShouldRemoveTTL() {
|
||||
|
||||
nativeConnection.setex(KEY_1, 10, VALUE_1);
|
||||
|
||||
@@ -427,6 +427,14 @@ public class LettuceClusterConnectionTests implements ClusterConnectionTests {
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void ttlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist() {
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES, TimeUnit.HOURS), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@@ -438,6 +446,17 @@ public class LettuceClusterConnectionTests implements ClusterConnectionTests {
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES), is(-1L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void ttlWithTimeUnitShouldReturnMinusOneWhenKeyDoesNotHaveExpirationSet() {
|
||||
|
||||
nativeConnection.set(KEY_1, VALUE_1);
|
||||
|
||||
assertThat(clusterConnection.ttl(KEY_1_BYTES, TimeUnit.SECONDS), is(-1L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
@@ -458,6 +477,14 @@ public class LettuceClusterConnectionTests implements ClusterConnectionTests {
|
||||
assertThat(clusterConnection.pTtl(KEY_1_BYTES), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
public void pTtlWithTimeUnitShouldReturnMinusTwoWhenKeyDoesNotExist() {
|
||||
assertThat(clusterConnection.pTtl(KEY_1_BYTES, TimeUnit.SECONDS), is(-2L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-315
|
||||
*/
|
||||
|
||||
@@ -128,6 +128,18 @@ public class RedisClusterTemplateTests<K, V> extends RedisTemplateTests<K, V> {
|
||||
super.testSortBulkMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("This one fails when using GET options on numbers")
|
||||
public void testGetExpireMillisUsingTransactions() {
|
||||
super.testGetExpireMillisUsingTransactions();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("This one fails when using GET options on numbers")
|
||||
public void testGetExpireMillisUsingPipelining() {
|
||||
super.testGetExpireMillisUsingPipelining();
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> testParams() {
|
||||
|
||||
|
||||
@@ -599,6 +599,65 @@ public class RedisTemplateTests<K, V> {
|
||||
assertThat(ttl, lessThan(25L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void testGetExpireMillisUsingTransactions() {
|
||||
|
||||
assumeTrue(redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory
|
||||
|| redisTemplate.getConnectionFactory() instanceof LettuceConnectionFactory);
|
||||
|
||||
final K key = keyFactory.instance();
|
||||
List<Object> result = redisTemplate.execute(new SessionCallback<List<Object>>() {
|
||||
|
||||
@Override
|
||||
public List<Object> execute(RedisOperations operations) throws DataAccessException {
|
||||
|
||||
operations.multi();
|
||||
operations.boundValueOps(key).set(valueFactory.instance());
|
||||
operations.expire(key, 1, TimeUnit.DAYS);
|
||||
operations.getExpire(key, TimeUnit.HOURS);
|
||||
|
||||
return operations.exec();
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(((Long) result.get(1)), greaterThanOrEqualTo(23L));
|
||||
assertThat(((Long) result.get(1)), lessThan(25L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-526
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void testGetExpireMillisUsingPipelining() {
|
||||
|
||||
assumeTrue(redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory
|
||||
|| redisTemplate.getConnectionFactory() instanceof LettuceConnectionFactory);
|
||||
|
||||
final K key = keyFactory.instance();
|
||||
List<Object> result = redisTemplate.executePipelined(new SessionCallback<Object>() {
|
||||
|
||||
@Override
|
||||
public Object execute(RedisOperations operations) throws DataAccessException {
|
||||
|
||||
operations.boundValueOps(key).set(valueFactory.instance());
|
||||
operations.expire(key, 1, TimeUnit.DAYS);
|
||||
operations.getExpire(key, TimeUnit.HOURS);
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(((Long) result.get(1)), greaterThanOrEqualTo(23L));
|
||||
assertThat(((Long) result.get(1)), lessThan(25L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetExpireMillisNotSupported() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user