Skip to content

Commit 3568f30

Browse files
committed
DATAREDIS-1106 - Polishing.
Add nullable annotations to methods that can return null. Update since tags. Extract phantom TTL to constant. Original pull request: spring-projects#521.
1 parent cca1f7a commit 3568f30

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
105105
implements InitializingBean, ApplicationContextAware, ApplicationListener<RedisKeyspaceEvent> {
106106

107+
/**
108+
* Time To Live in seconds that phantom keys should live longer than the actual key.
109+
*/
110+
private static final int PHANTOM_KEY_TTL = 300;
111+
107112
private RedisOperations<?, ?> redisOps;
108113
private RedisConverter converter;
109114
private @Nullable RedisMessageListenerContainer messageListenerContainer;
@@ -201,7 +206,7 @@ protected RedisKeyValueAdapter() {}
201206
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#put(java.lang.Object, java.lang.Object, java.lang.String)
202207
*/
203208
@Override
204-
public Object put(final Object id, Object item, String keyspace) {
209+
public Object put(Object id, Object item, String keyspace) {
205210

206211
RedisData rdo = item instanceof RedisData ? (RedisData) item : new RedisData();
207212
if (!(item instanceof RedisData)) {
@@ -237,7 +242,7 @@ public Object put(final Object id, Object item, String keyspace) {
237242
byte[] phantomKey = ByteUtils.concat(objectKey, BinaryKeyspaceIdentifier.PHANTOM_SUFFIX);
238243
connection.del(phantomKey);
239244
connection.hMSet(phantomKey, rdo.getBucket().rawMap());
240-
connection.expire(phantomKey, rdo.getTimeToLive() + 300);
245+
connection.expire(phantomKey, rdo.getTimeToLive() + PHANTOM_KEY_TTL);
241246
}
242247

243248
connection.sAdd(toBytes(rdo.getKeyspace()), key);
@@ -473,7 +478,7 @@ public void update(PartialUpdate<?> update) {
473478
// add phantom key so values can be restored
474479
byte[] phantomKey = ByteUtils.concat(redisKey, BinaryKeyspaceIdentifier.PHANTOM_SUFFIX);
475480
connection.hMSet(phantomKey, rdo.getBucket().rawMap());
476-
connection.expire(phantomKey, rdo.getTimeToLive() + 300);
481+
connection.expire(phantomKey, rdo.getTimeToLive() + PHANTOM_KEY_TTL);
477482

478483
} else {
479484

src/main/java/org/springframework/data/redis/core/TimeToLiveAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface TimeToLiveAccessor {
3535
/**
3636
* @param type must not be {@literal null}.
3737
* @return return {@literal true} if the entity could potentially expire.
38-
* @since ? (depends on backport)
38+
* @since 2.3
3939
*/
4040
boolean isExpiringEntity(Class<?> type);
4141
}

src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static class ConfigAwareTimeToLiveAccessor implements TimeToLiveAccessor {
208208

209209
@Override
210210
@SuppressWarnings({ "rawtypes" })
211-
public Long getTimeToLive(final Object source) {
211+
public Long getTimeToLive(Object source) {
212212

213213
Assert.notNull(source, "Source must not be null!");
214214
Class<?> type = source instanceof Class<?> ? (Class<?>) source
@@ -220,8 +220,7 @@ public Long getTimeToLive(final Object source) {
220220
PersistentProperty<?> ttlProperty = resolveTtlProperty(type);
221221

222222
if (ttlProperty != null && ttlProperty.isAnnotationPresent(TimeToLive.class)) {
223-
224-
unit = ttlProperty.findAnnotation(TimeToLive.class).unit();
223+
unit = ttlProperty.getRequiredAnnotation(TimeToLive.class).unit();
225224
}
226225

227226
if (source instanceof PartialUpdate) {
@@ -251,6 +250,7 @@ public Long getTimeToLive(final Object source) {
251250
} else {
252251

253252
Method timeoutMethod = resolveTimeMethod(type);
253+
254254
if (timeoutMethod != null) {
255255

256256
if (!timeoutMethod.isAccessible()) {
@@ -260,7 +260,7 @@ public Long getTimeToLive(final Object source) {
260260
TimeToLive ttl = AnnotationUtils.findAnnotation(timeoutMethod, TimeToLive.class);
261261
try {
262262
Number timeout = (Number) timeoutMethod.invoke(source);
263-
if (timeout != null) {
263+
if (timeout != null && ttl != null) {
264264
return TimeUnit.SECONDS.convert(timeout.longValue(), ttl.unit());
265265
}
266266
} catch (IllegalAccessException e) {
@@ -291,12 +291,15 @@ public boolean isExpiringEntity(Class<?> type) {
291291
if (defaultTimeOut != null && defaultTimeOut > 0) {
292292
return true;
293293
}
294+
294295
if (resolveTtlProperty(type) != null) {
295296
return true;
296297
}
298+
297299
return resolveTimeMethod(type) != null;
298300
}
299301

302+
@Nullable
300303
private Long resolveDefaultTimeOut(Class<?> type) {
301304

302305
if (this.defaultTimeouts.containsKey(type)) {
@@ -318,7 +321,8 @@ private Long resolveDefaultTimeOut(Class<?> type) {
318321
return defaultTimeout;
319322
}
320323

321-
@SuppressWarnings({ "rawtypes", "unchecked" })
324+
@SuppressWarnings({ "rawtypes" })
325+
@Nullable
322326
private PersistentProperty<?> resolveTtlProperty(Class<?> type) {
323327

324328
if (timeoutProperties.containsKey(type)) {
@@ -351,7 +355,8 @@ private PersistentProperty<?> resolveTtlProperty(Class<?> type) {
351355
return null;
352356
}
353357

354-
private Method resolveTimeMethod(final Class<?> type) {
358+
@Nullable
359+
private Method resolveTimeMethod(Class<?> type) {
355360

356361
if (timeoutMethods.containsKey(type)) {
357362
return timeoutMethods.get(type);

src/main/java/org/springframework/data/redis/core/mapping/RedisPersistentEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T, Re
4646
/**
4747
* Get the {@link PersistentProperty} that is annotated with {@link org.springframework.data.redis.core.TimeToLive}.
4848
*
49-
* @return can be {@null}.
49+
* @return can be {@literal null}.
5050
* @since 1.8
5151
*/
5252
@Nullable
5353
RedisPersistentProperty getExplicitTimeToLiveProperty();
5454

5555
/**
5656
* @return {@literal true} if the entity could potentially expire.
57-
* @since ? (depends on backport)
57+
* @since 2.3
5858
*/
5959
default boolean isExpiring() {
6060
return getTimeToLiveAccessor().isExpiringEntity(getType());

0 commit comments

Comments
 (0)