Skip to content

Commit c00fdf7

Browse files
authored
Do not clear TTL when incr/decr a key. (#69)
According to the Redis spec, TTL should not be cleared when incrementing/decrementing a value. There are many other operations that should not clear the TTL, this is just addressing incr, incrBy, decr, decrBy. Fixes #25
1 parent 188af98 commit c00fdf7

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

src/main/java/com/github/fppt/jedismock/operations/RO_incrOrDecrBy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Slice response() {
2525
}
2626

2727
long r = convertToLong(new String(v.data())) + d;
28-
base().putValue(key, Slice.create(String.valueOf(r)));
28+
base().putValueWithoutClearingTtl(key, Slice.create(String.valueOf(r)));
2929
return Response.integer(r);
3030
}
3131
}

src/main/java/com/github/fppt/jedismock/storage/ExpiringKeyValueStorage.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ public void put(Slice key1, Slice key2, Slice value, Long ttl){
101101
Preconditions.checkNotNull(value);
102102

103103
values().put(key1, key2, value);
104-
if (ttl != null) {
104+
if (ttl == null) {
105+
// If a TTL hasn't been provided, we don't want to override the TTL. However, if no TTL is set for this key,
106+
// we should still set it to -1L
107+
if (getTTL(key1, key2) == null) {
108+
setDeadline(key1, key2, -1L);
109+
}
110+
} else {
105111
if (ttl != -1) {
106112
setTTL(key1, key2, ttl);
107113
} else {

src/main/java/com/github/fppt/jedismock/storage/RedisBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public void clear(){
5353
subscribers.clear();
5454
}
5555

56+
public void putValueWithoutClearingTtl(Slice key, Slice value) {
57+
putValue(key, value, null);
58+
}
59+
60+
public void putValueWithoutClearingTtl(Slice key1, Slice key2, Slice value) {
61+
putValue(key1, key2, value, null);
62+
}
63+
5664
public void putValue(Slice key, Slice value){
5765
putValue(key, value, -1L);
5866
}

src/test/java/com/github/fppt/jedismock/comparisontests/SimpleOperationsTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,4 +853,60 @@ public void zrangebylexKeysCorrectOrder(Jedis jedis) {
853853
assertEquals("bbb", results.get(0));
854854
assertEquals("ccc", results.get(1));
855855
}
856+
857+
@Theory
858+
public void incrDoesNotClearTtl(Jedis jedis) {
859+
jedis.flushDB();
860+
861+
String key = "mykey";
862+
jedis.set(key, "0");
863+
jedis.expire(key, 100);
864+
865+
jedis.incr(key);
866+
long ttl = jedis.ttl(key);
867+
868+
assertTrue(ttl > 0);
869+
}
870+
871+
@Theory
872+
public void incrByDoesNotClearTtl(Jedis jedis) {
873+
jedis.flushDB();
874+
875+
String key = "mykey";
876+
jedis.set(key, "0");
877+
jedis.expire(key, 100);
878+
879+
jedis.incrBy(key, 10);
880+
long ttl = jedis.ttl(key);
881+
882+
assertTrue(ttl > 0);
883+
}
884+
885+
@Theory
886+
public void decrDoesNotClearTtl(Jedis jedis) {
887+
jedis.flushDB();
888+
889+
String key = "mykey";
890+
jedis.set(key, "0");
891+
jedis.expire(key, 100);
892+
893+
jedis.decr(key);
894+
long ttl = jedis.ttl(key);
895+
896+
assertTrue(ttl > 0);
897+
}
898+
899+
@Theory
900+
public void decrByDoesNotClearTtl(Jedis jedis) {
901+
jedis.flushDB();
902+
903+
String key = "mykey";
904+
jedis.set(key, "0");
905+
jedis.expire(key, 100);
906+
907+
jedis.decrBy(key, 10);
908+
long ttl = jedis.ttl(key);
909+
910+
assertTrue(ttl > 0);
911+
}
856912
}

0 commit comments

Comments
 (0)