Skip to content

Commit 59582f1

Browse files
Thomas Darimontchristophstrobl
Thomas Darimont
authored andcommitted
DATAREDIS-286 - Avoid overflow in JedisConnection#expire / #pexpire.
Workaround Jedis issue redis/jedis#575. We now delegate calls to expire(...) to pExpireAt(...) in case the seconds parameter is > than Integer.MAX_VALUE. We query the server time to compute the new expiration date. Original pull request: #52.
1 parent 408b52a commit 59582f1

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Properties;
2727
import java.util.Queue;
2828
import java.util.Set;
29+
import java.util.concurrent.TimeUnit;
2930

3031
import org.springframework.core.convert.converter.Converter;
3132
import org.springframework.dao.DataAccessException;
@@ -737,6 +738,18 @@ public Boolean exists(byte[] key) {
737738
}
738739

739740
public Boolean expire(byte[] key, long seconds) {
741+
742+
/*
743+
* @see DATAREDIS-286 to avoid overflow in Jedis
744+
*
745+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
746+
* fix for: https://github.com/xetorthio/jedis/pull/575
747+
*/
748+
if (seconds > Integer.MAX_VALUE) {
749+
750+
return pExpireAt(key, time() + TimeUnit.SECONDS.toMillis(seconds));
751+
}
752+
740753
try {
741754
if (isPipelined()) {
742755
pipeline(new JedisResult(pipeline.expire(key, (int) seconds), JedisConverters.longToBoolean()));
@@ -912,6 +925,18 @@ public Long ttl(byte[] key) {
912925
}
913926

914927
public Boolean pExpire(byte[] key, long millis) {
928+
929+
/*
930+
* @see DATAREDIS-286 to avoid overflow in Jedis
931+
*
932+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
933+
* fix for: https://github.com/xetorthio/jedis/pull/575
934+
*/
935+
if (millis > Integer.MAX_VALUE) {
936+
937+
return pExpireAt(key, time() + millis);
938+
}
939+
915940
try {
916941
if (isPipelined()) {
917942
pipeline(new JedisResult(pipeline.pexpire(key, (int) millis), JedisConverters.longToBoolean()));

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.data.redis.connection.jedis;
1818

19+
import static org.hamcrest.CoreMatchers.*;
1920
import static org.junit.Assert.*;
2021

2122
import java.util.HashSet;
@@ -333,4 +334,35 @@ public void testExecuteShouldConvertArrayReplyCorrectly() {
333334
AllOf.allOf(IsInstanceOf.instanceOf(List.class), IsCollectionContaining.hasItems("awesome".getBytes(),
334335
"cool".getBytes(), "supercalifragilisticexpialidocious".getBytes())));
335336
}
337+
338+
/**
339+
* @see DATAREDIS-286
340+
*/
341+
@Test
342+
public void expireShouldSupportExiprationForValuesLargerThanInteger() {
343+
344+
connection.set("expireKey", "foo");
345+
346+
long seconds = ((long) Integer.MAX_VALUE) + 1;
347+
connection.expire("expireKey", seconds);
348+
long ttl = connection.ttl("expireKey");
349+
350+
assertThat(ttl, is(seconds));
351+
}
352+
353+
/**
354+
* @see DATAREDIS-286
355+
*/
356+
@Test
357+
public void pExpireShouldSupportExiprationForValuesLargerThanInteger() {
358+
359+
connection.set("pexpireKey", "foo");
360+
361+
long millis = ((long) Integer.MAX_VALUE) + 10;
362+
connection.pExpire("pexpireKey", millis);
363+
long ttl = connection.pTtl("pexpireKey");
364+
365+
assertTrue(String.format("difference between millis=%s and ttl=%s should not be greater than 20ms but is %s",
366+
millis, ttl, millis - ttl), millis - ttl < 20L);
367+
}
336368
}

0 commit comments

Comments
 (0)