Skip to content

Commit e8a30a7

Browse files
committed
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.
1 parent 5a3a754 commit e8a30a7

22 files changed

+816
-17
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 73 additions & 5 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.apache.commons.logging.Log;
3132
import org.apache.commons.logging.LogFactory;
@@ -938,14 +939,35 @@ public Long sUnionStore(byte[] destKey, byte[]... keys) {
938939
return result;
939940
}
940941

942+
/*
943+
* (non-Javadoc)
944+
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[])
945+
*/
946+
@Override
941947
public Long ttl(byte[] key) {
948+
942949
Long result = delegate.ttl(key);
943950
if (isFutureConversion()) {
944951
addResultConverter(identityConverter);
945952
}
946953
return result;
947954
}
948955

956+
/*
957+
* (non-Javadoc)
958+
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[], java.util.concurrent.TimeUnit)
959+
*/
960+
@Override
961+
public Long ttl(byte[] key, TimeUnit timeUnit) {
962+
963+
Long result = delegate.ttl(key, timeUnit);
964+
if (isFutureConversion()) {
965+
addResultConverter(identityConverter);
966+
}
967+
968+
return result;
969+
}
970+
949971
public DataType type(byte[] key) {
950972
DataType result = delegate.type(key);
951973
if (isFutureConversion()) {
@@ -1329,11 +1351,33 @@ public Boolean pExpireAt(byte[] key, long unixTimeInMillis) {
13291351
return result;
13301352
}
13311353

1354+
/*
1355+
* (non-Javadoc)
1356+
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[])
1357+
*/
1358+
@Override
13321359
public Long pTtl(byte[] key) {
1360+
13331361
Long result = delegate.pTtl(key);
13341362
if (isFutureConversion()) {
13351363
addResultConverter(identityConverter);
13361364
}
1365+
1366+
return result;
1367+
}
1368+
1369+
/*
1370+
* (non-Javadoc)
1371+
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[], java.util.concurrent.TimeUnit)
1372+
*/
1373+
@Override
1374+
public Long pTtl(byte[] key, TimeUnit timeUnit) {
1375+
1376+
Long result = delegate.pTtl(key, timeUnit);
1377+
if (isFutureConversion()) {
1378+
addResultConverter(identityConverter);
1379+
}
1380+
13371381
return result;
13381382
}
13391383

@@ -2059,12 +2103,22 @@ public Long sUnionStore(String destKey, String... keys) {
20592103
return result;
20602104
}
20612105

2106+
/*
2107+
* (non-Javadoc)
2108+
* @see org.springframework.data.redis.connection.StringRedisConnection#ttl(java.lang.String)
2109+
*/
2110+
@Override
20622111
public Long ttl(String key) {
2063-
Long result = delegate.ttl(serialize(key));
2064-
if (isFutureConversion()) {
2065-
addResultConverter(identityConverter);
2066-
}
2067-
return result;
2112+
return ttl(serialize(key));
2113+
}
2114+
2115+
/*
2116+
* (non-Javadoc)
2117+
* @see org.springframework.data.redis.connection.StringRedisConnection#ttl(java.lang.String, java.util.concurrent.TimeUnit)
2118+
*/
2119+
@Override
2120+
public Long ttl(String key, TimeUnit timeUnit) {
2121+
return ttl(serialize(key), timeUnit);
20682122
}
20692123

20702124
public DataType type(String key) {
@@ -2689,10 +2743,24 @@ public Boolean pExpireAt(String key, long unixTimeInMillis) {
26892743
return pExpireAt(serialize(key), unixTimeInMillis);
26902744
}
26912745

2746+
/*
2747+
* (non-Javadoc)
2748+
* @see org.springframework.data.redis.connection.StringRedisConnection#pTtl(java.lang.String)
2749+
*/
2750+
@Override
26922751
public Long pTtl(String key) {
26932752
return pTtl(serialize(key));
26942753
}
26952754

2755+
/*
2756+
* (non-Javadoc)
2757+
* @see org.springframework.data.redis.connection.StringRedisConnection#pTtl(java.lang.String, java.util.concurrent.TimeUnit)
2758+
*/
2759+
@Override
2760+
public Long pTtl(String key, TimeUnit timeUnit) {
2761+
return pTtl(serialize(key), timeUnit);
2762+
}
2763+
26962764
public String scriptLoad(String script) {
26972765
String result = delegate.scriptLoad(serialize(script));
26982766
if (isFutureConversion()) {

src/main/java/org/springframework/data/redis/connection/RedisKeyCommands.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import java.util.List;
1919
import java.util.Set;
20+
import java.util.concurrent.TimeUnit;
2021

2122
import org.springframework.data.redis.core.Cursor;
2223
import org.springframework.data.redis.core.ScanOptions;
@@ -26,6 +27,7 @@
2627
*
2728
* @author Costin Leau
2829
* @author Christoph Strobl
30+
* @author Mark Paluch
2931
*/
3032
public interface RedisKeyCommands {
3133

@@ -186,7 +188,19 @@ public interface RedisKeyCommands {
186188
Long ttl(byte[] key);
187189

188190
/**
189-
* Get the time to live for {@code key} in milliseconds.
191+
* Get the time to live for {@code key} in and convert it to the given {@link TimeUnit}.
192+
* <p>
193+
* See http://redis.io/commands/ttl
194+
*
195+
* @param key
196+
* @param timeUnit
197+
* @return
198+
* @since 1.8
199+
*/
200+
Long ttl(byte[] key, TimeUnit timeUnit);
201+
202+
/**
203+
* Get the precise time to live for {@code key} in milliseconds.
190204
* <p>
191205
* See http://redis.io/commands/pttl
192206
*
@@ -195,6 +209,18 @@ public interface RedisKeyCommands {
195209
*/
196210
Long pTtl(byte[] key);
197211

212+
/**
213+
* Get the precise time to live for {@code key} in and convert it to the given {@link TimeUnit}.
214+
* <p>
215+
* See http://redis.io/commands/pttl
216+
*
217+
* @param key
218+
* @param timeUnit
219+
* @return
220+
* @since 1.8
221+
*/
222+
Long pTtl(byte[] key, TimeUnit timeUnit);
223+
198224
/**
199225
* Sort the elements for {@code key}.
200226
* <p>

src/main/java/org/springframework/data/redis/connection/StringRedisConnection.java

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121
import java.util.Set;
22+
import java.util.concurrent.TimeUnit;
2223

2324
import org.springframework.data.geo.Circle;
2425
import org.springframework.data.geo.Distance;
@@ -60,34 +61,176 @@ public interface StringTuple extends Tuple {
6061

6162
Object execute(String command);
6263

64+
/**
65+
* Determine if given {@code key} exists.
66+
* <p>
67+
* See http://redis.io/commands/exists
68+
*
69+
* @param key
70+
* @return
71+
*/
6372
Boolean exists(String key);
6473

74+
/**
75+
* Delete given {@code keys}.
76+
* <p>
77+
* See http://redis.io/commands/del
78+
*
79+
* @param keys
80+
* @return The number of keys that were removed.
81+
*/
6582
Long del(String... keys);
6683

84+
/**
85+
* Determine the type stored at {@code key}.
86+
* <p>
87+
* See http://redis.io/commands/type
88+
*
89+
* @param key
90+
* @return
91+
*/
6792
DataType type(String key);
6893

94+
/**
95+
* Find all keys matching the given {@code pattern}.
96+
* <p>
97+
* See http://redis.io/commands/keys
98+
*
99+
* @param pattern
100+
* @return
101+
*/
69102
Collection<String> keys(String pattern);
70103

104+
/**
105+
* Rename key {@code oleName} to {@code newName}.
106+
* <p>
107+
* See http://redis.io/commands/rename
108+
*
109+
* @param oldName
110+
* @param newName
111+
*/
71112
void rename(String oldName, String newName);
72113

114+
/**
115+
* Rename key {@code oleName} to {@code newName} only if {@code newName} does not exist.
116+
* <p>
117+
* See http://redis.io/commands/renamenx
118+
*
119+
* @param oldName
120+
* @param newName
121+
* @return
122+
*/
73123
Boolean renameNX(String oldName, String newName);
74124

125+
/**
126+
* Set time to live for given {@code key} in seconds.
127+
* <p>
128+
* See http://redis.io/commands/expire
129+
*
130+
* @param key
131+
* @param seconds
132+
* @return
133+
*/
75134
Boolean expire(String key, long seconds);
76135

136+
/**
137+
* Set time to live for given {@code key} in milliseconds.
138+
* <p>
139+
* See http://redis.io/commands/pexpire
140+
*
141+
* @param key
142+
* @param millis
143+
* @return
144+
*/
77145
Boolean pExpire(String key, long millis);
78146

147+
/**
148+
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp.
149+
* <p>
150+
* See http://redis.io/commands/expireat
151+
*
152+
* @param key
153+
* @param unixTime
154+
* @return
155+
*/
79156
Boolean expireAt(String key, long unixTime);
80157

158+
/**
159+
* Set the expiration for given {@code key} as a {@literal UNIX} timestamp in milliseconds.
160+
* <p>
161+
* See http://redis.io/commands/pexpireat
162+
*
163+
* @param key
164+
* @param unixTimeInMillis
165+
* @return
166+
*/
81167
Boolean pExpireAt(String key, long unixTimeInMillis);
82168

169+
/**
170+
* Remove the expiration from given {@code key}.
171+
* <p>
172+
* See http://redis.io/commands/persist
173+
*
174+
* @param key
175+
* @return
176+
*/
83177
Boolean persist(String key);
84178

179+
/**
180+
* Move given {@code key} to database with {@code index}.
181+
* <p>
182+
* See http://redis.io/commands/move
183+
*
184+
* @param key
185+
* @param dbIndex
186+
* @return
187+
*/
85188
Boolean move(String key, int dbIndex);
86189

190+
/**
191+
* Get the time to live for {@code key} in seconds.
192+
* <p>
193+
* See http://redis.io/commands/ttl
194+
*
195+
* @param key
196+
* @return
197+
*/
87198
Long ttl(String key);
88199

200+
/**
201+
* Get the time to live for {@code key} in and convert it to the given {@link TimeUnit}.
202+
* <p>
203+
* See http://redis.io/commands/ttl
204+
*
205+
* @param key
206+
* @param timeUnit
207+
* @return
208+
* @since 1.8
209+
*/
210+
Long ttl(String key, TimeUnit timeUnit);
211+
212+
/**
213+
* Get the precise time to live for {@code key} in milliseconds.
214+
* <p>
215+
* See http://redis.io/commands/pttl
216+
*
217+
* @param key
218+
* @return
219+
*/
89220
Long pTtl(String key);
90221

222+
/**
223+
* Get the precise time to live for {@code key} in and convert it to the given {@link TimeUnit}.
224+
* <p>
225+
* See http://redis.io/commands/pttl
226+
*
227+
* @param key
228+
* @param timeUnit
229+
* @return
230+
* @since 1.8
231+
*/
232+
Long pTtl(String key, TimeUnit timeUnit);
233+
91234
String echo(String message);
92235

93236
// sort commands

0 commit comments

Comments
 (0)