Skip to content

Commit fd6e4d1

Browse files
committed
DATAREDIS-523 - Polishing.
Allow reading of the TTL into primitive properties. Allow general numeric types for TTL properties. Extend date range in license headers. Extend JavaDoc. Original pull request: #208.
1 parent c4ddc9e commit fd6e4d1

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ public Long doInRedis(RedisConnection connection) throws DataAccessException {
605605
}
606606
});
607607

608-
if (timeout != null || (timeout == null && !ttlProperty.getType().isPrimitive())) {
608+
if (timeout != null || !ttlProperty.getType().isPrimitive()) {
609609
entity.getPropertyAccessor(target).setProperty(ttlProperty,
610-
NumberUtils.convertNumberToTargetClass(timeout, (Class) ttlProperty.getType()));
610+
converter.getConversionService().convert(timeout, ttlProperty.getType()));
611611
}
612612
}
613613

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public Long getTimeToLive(final Object source) {
282282
} else if (ttlProperty != null) {
283283

284284
RedisPersistentEntity entity = mappingContext.getPersistentEntity(type);
285-
Long timeout = (Long) entity.getPropertyAccessor(source).getProperty(ttlProperty);
285+
Number timeout = (Number) entity.getPropertyAccessor(source).getProperty(ttlProperty);
286286
if (timeout != null) {
287-
return TimeUnit.SECONDS.convert(timeout, unit);
287+
return TimeUnit.SECONDS.convert(timeout.longValue(), unit);
288288
}
289289

290290
} else {

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
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-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.
@@ -43,7 +43,7 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T> {
4343
boolean hasExplictTimeToLiveProperty();
4444

4545
/**
46-
* Get the {@link PersistentProperty}
46+
* Get the {@link PersistentProperty} that is annotated with {@link org.springframework.data.redis.core.TimeToLive}.
4747
*
4848
* @return can be {@null}.
4949
* @since 1.8

src/test/java/org/springframework/data/redis/core/RedisKeyValueTemplateTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-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.
@@ -48,7 +48,10 @@
4848
import lombok.EqualsAndHashCode;
4949

5050
/**
51+
* Integration tests for {@link RedisKeyValueTemplate}.
52+
*
5153
* @author Christoph Strobl
54+
* @author Mark Paluch
5255
*/
5356
@RunWith(Parameterized.class)
5457
public class RedisKeyValueTemplateTests {
@@ -887,6 +890,25 @@ public void shouldReadBackExplicitTimeToLive() throws InterruptedException {
887890
assertThat(target.ttl.doubleValue(), is(closeTo(3D, 1D)));
888891
}
889892

893+
/**
894+
* @see DATAREDIS-523
895+
*/
896+
@Test
897+
public void shouldReadBackExplicitTimeToLiveToPrimitiveField() throws InterruptedException {
898+
899+
WithPrimitiveTtl source = new WithPrimitiveTtl();
900+
source.id = "ttl-1";
901+
source.ttl = 5;
902+
source.value = "5 seconds";
903+
904+
template.insert(source);
905+
906+
Thread.sleep(1100);
907+
908+
WithPrimitiveTtl target = template.findById(source.id, WithPrimitiveTtl.class);
909+
assertThat((double) target.ttl, is(closeTo(3D, 1D)));
910+
}
911+
890912
/**
891913
* @see DATAREDIS-523
892914
*/
@@ -1049,4 +1071,12 @@ static class WithTtl {
10491071
String value;
10501072
@TimeToLive Long ttl;
10511073
}
1074+
1075+
@Data
1076+
static class WithPrimitiveTtl {
1077+
1078+
@Id String id;
1079+
String value;
1080+
@TimeToLive int ttl;
1081+
}
10521082
}

0 commit comments

Comments
 (0)