diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java index 26d51b338..5c5216798 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Properties; import java.util.Queue; import java.util.Set; +import java.util.concurrent.TimeUnit; import org.springframework.core.convert.converter.Converter; import org.springframework.dao.DataAccessException; @@ -737,6 +738,18 @@ public class JedisConnection implements RedisConnection { } public Boolean expire(byte[] key, long seconds) { + + /* + * @see DATAREDIS-286 to avoid overflow in Jedis + * + * TODO Remove this workaround when we upgrade to a Jedis version that contains a + * fix for: https://github.com/xetorthio/jedis/pull/575 + */ + if (seconds > Integer.MAX_VALUE) { + + return pExpireAt(key, time() + TimeUnit.SECONDS.toMillis(seconds)); + } + try { if (isPipelined()) { pipeline(new JedisResult(pipeline.expire(key, (int) seconds), JedisConverters.longToBoolean())); @@ -912,6 +925,18 @@ public class JedisConnection implements RedisConnection { } public Boolean pExpire(byte[] key, long millis) { + + /* + * @see DATAREDIS-286 to avoid overflow in Jedis + * + * TODO Remove this workaround when we upgrade to a Jedis version that contains a + * fix for: https://github.com/xetorthio/jedis/pull/575 + */ + if (millis > Integer.MAX_VALUE) { + + return pExpireAt(key, time() + millis); + } + try { if (isPipelined()) { pipeline(new JedisResult(pipeline.pexpire(key, (int) millis), JedisConverters.longToBoolean())); diff --git a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java index 3419df7b6..026a9a6d8 100644 --- a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java @@ -16,6 +16,7 @@ package org.springframework.data.redis.connection.jedis; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.util.HashSet; @@ -333,4 +334,35 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati AllOf.allOf(IsInstanceOf.instanceOf(List.class), IsCollectionContaining.hasItems("awesome".getBytes(), "cool".getBytes(), "supercalifragilisticexpialidocious".getBytes()))); } + + /** + * @see DATAREDIS-286 + */ + @Test + public void expireShouldSupportExiprationForValuesLargerThanInteger() { + + connection.set("expireKey", "foo"); + + long seconds = ((long) Integer.MAX_VALUE) + 1; + connection.expire("expireKey", seconds); + long ttl = connection.ttl("expireKey"); + + assertThat(ttl, is(seconds)); + } + + /** + * @see DATAREDIS-286 + */ + @Test + public void pExpireShouldSupportExiprationForValuesLargerThanInteger() { + + connection.set("pexpireKey", "foo"); + + long millis = ((long) Integer.MAX_VALUE) + 10; + connection.pExpire("pexpireKey", millis); + long ttl = connection.pTtl("pexpireKey"); + + assertTrue(String.format("difference between millis=%s and ttl=%s should not be greater than 20ms but is %s", + millis, ttl, millis - ttl), millis - ttl < 20L); + } }