Skip to content

Commit 2806567

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 f8806c8 commit 2806567

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;
@@ -704,6 +705,18 @@ public Boolean exists(byte[] key) {
704705
}
705706

706707
public Boolean expire(byte[] key, long seconds) {
708+
709+
/*
710+
* @see DATAREDIS-286 to avoid overflow in Jedis
711+
*
712+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
713+
* fix for: https://github.com/xetorthio/jedis/pull/575
714+
*/
715+
if (seconds > Integer.MAX_VALUE) {
716+
717+
return pExpireAt(key, time() + TimeUnit.SECONDS.toMillis(seconds));
718+
}
719+
707720
try {
708721
if (isPipelined()) {
709722
pipeline(new JedisResult(pipeline.expire(key, (int) seconds), JedisConverters.longToBoolean()));
@@ -879,6 +892,18 @@ public Long ttl(byte[] key) {
879892
}
880893

881894
public Boolean pExpire(byte[] key, long millis) {
895+
896+
/*
897+
* @see DATAREDIS-286 to avoid overflow in Jedis
898+
*
899+
* TODO Remove this workaround when we upgrade to a Jedis version that contains a
900+
* fix for: https://github.com/xetorthio/jedis/pull/575
901+
*/
902+
if (millis > Integer.MAX_VALUE) {
903+
904+
return pExpireAt(key, time() + millis);
905+
}
906+
882907
try {
883908
if (isPipelined()) {
884909
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)