DATAREDIS-1106 - Polishing.
Add nullable annotations to methods that can return null. Update since tags. Extract phantom TTL to constant. Original pull request: #521.
This commit is contained in:
@@ -104,6 +104,11 @@ import org.springframework.util.ObjectUtils;
|
||||
public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
implements InitializingBean, ApplicationContextAware, ApplicationListener<RedisKeyspaceEvent> {
|
||||
|
||||
/**
|
||||
* Time To Live in seconds that phantom keys should live longer than the actual key.
|
||||
*/
|
||||
private static final int PHANTOM_KEY_TTL = 300;
|
||||
|
||||
private RedisOperations<?, ?> redisOps;
|
||||
private RedisConverter converter;
|
||||
private @Nullable RedisMessageListenerContainer messageListenerContainer;
|
||||
@@ -201,7 +206,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
* @see org.springframework.data.keyvalue.core.KeyValueAdapter#put(java.lang.Object, java.lang.Object, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Object put(final Object id, Object item, String keyspace) {
|
||||
public Object put(Object id, Object item, String keyspace) {
|
||||
|
||||
RedisData rdo = item instanceof RedisData ? (RedisData) item : new RedisData();
|
||||
if (!(item instanceof RedisData)) {
|
||||
@@ -237,7 +242,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
byte[] phantomKey = ByteUtils.concat(objectKey, BinaryKeyspaceIdentifier.PHANTOM_SUFFIX);
|
||||
connection.del(phantomKey);
|
||||
connection.hMSet(phantomKey, rdo.getBucket().rawMap());
|
||||
connection.expire(phantomKey, rdo.getTimeToLive() + 300);
|
||||
connection.expire(phantomKey, rdo.getTimeToLive() + PHANTOM_KEY_TTL);
|
||||
}
|
||||
|
||||
connection.sAdd(toBytes(rdo.getKeyspace()), key);
|
||||
@@ -473,7 +478,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
|
||||
// add phantom key so values can be restored
|
||||
byte[] phantomKey = ByteUtils.concat(redisKey, BinaryKeyspaceIdentifier.PHANTOM_SUFFIX);
|
||||
connection.hMSet(phantomKey, rdo.getBucket().rawMap());
|
||||
connection.expire(phantomKey, rdo.getTimeToLive() + 300);
|
||||
connection.expire(phantomKey, rdo.getTimeToLive() + PHANTOM_KEY_TTL);
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ public interface TimeToLiveAccessor {
|
||||
/**
|
||||
* @param type must not be {@literal null}.
|
||||
* @return return {@literal true} if the entity could potentially expire.
|
||||
* @since ? (depends on backport)
|
||||
* @since 2.3
|
||||
*/
|
||||
boolean isExpiringEntity(Class<?> type);
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public Long getTimeToLive(final Object source) {
|
||||
public Long getTimeToLive(Object source) {
|
||||
|
||||
Assert.notNull(source, "Source must not be null!");
|
||||
Class<?> type = source instanceof Class<?> ? (Class<?>) source
|
||||
@@ -220,8 +220,7 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
PersistentProperty<?> ttlProperty = resolveTtlProperty(type);
|
||||
|
||||
if (ttlProperty != null && ttlProperty.isAnnotationPresent(TimeToLive.class)) {
|
||||
|
||||
unit = ttlProperty.findAnnotation(TimeToLive.class).unit();
|
||||
unit = ttlProperty.getRequiredAnnotation(TimeToLive.class).unit();
|
||||
}
|
||||
|
||||
if (source instanceof PartialUpdate) {
|
||||
@@ -251,6 +250,7 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
} else {
|
||||
|
||||
Method timeoutMethod = resolveTimeMethod(type);
|
||||
|
||||
if (timeoutMethod != null) {
|
||||
|
||||
if (!timeoutMethod.isAccessible()) {
|
||||
@@ -260,7 +260,7 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
TimeToLive ttl = AnnotationUtils.findAnnotation(timeoutMethod, TimeToLive.class);
|
||||
try {
|
||||
Number timeout = (Number) timeoutMethod.invoke(source);
|
||||
if (timeout != null) {
|
||||
if (timeout != null && ttl != null) {
|
||||
return TimeUnit.SECONDS.convert(timeout.longValue(), ttl.unit());
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
@@ -291,12 +291,15 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
if (defaultTimeOut != null && defaultTimeOut > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (resolveTtlProperty(type) != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return resolveTimeMethod(type) != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Long resolveDefaultTimeOut(Class<?> type) {
|
||||
|
||||
if (this.defaultTimeouts.containsKey(type)) {
|
||||
@@ -318,7 +321,8 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
return defaultTimeout;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
@Nullable
|
||||
private PersistentProperty<?> resolveTtlProperty(Class<?> type) {
|
||||
|
||||
if (timeoutProperties.containsKey(type)) {
|
||||
@@ -351,7 +355,8 @@ public class RedisMappingContext extends KeyValueMappingContext<RedisPersistentE
|
||||
return null;
|
||||
}
|
||||
|
||||
private Method resolveTimeMethod(final Class<?> type) {
|
||||
@Nullable
|
||||
private Method resolveTimeMethod(Class<?> type) {
|
||||
|
||||
if (timeoutMethods.containsKey(type)) {
|
||||
return timeoutMethods.get(type);
|
||||
|
||||
@@ -46,7 +46,7 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T, Re
|
||||
/**
|
||||
* Get the {@link PersistentProperty} that is annotated with {@link org.springframework.data.redis.core.TimeToLive}.
|
||||
*
|
||||
* @return can be {@null}.
|
||||
* @return can be {@literal null}.
|
||||
* @since 1.8
|
||||
*/
|
||||
@Nullable
|
||||
@@ -54,7 +54,7 @@ public interface RedisPersistentEntity<T> extends KeyValuePersistentEntity<T, Re
|
||||
|
||||
/**
|
||||
* @return {@literal true} if the entity could potentially expire.
|
||||
* @since ? (depends on backport)
|
||||
* @since 2.3
|
||||
*/
|
||||
default boolean isExpiring() {
|
||||
return getTimeToLiveAccessor().isExpiringEntity(getType());
|
||||
|
||||
Reference in New Issue
Block a user