DATAREDIS-317 - Report inappropriate RedisTemplate more eagerly in RedisAtomic wrappers.

We now validate the given RedisTemplate in the constructor of RedisAtomicLong/RedisAtomicInteger/RedisAtomicDouble to report an inappropriate RedisTemplate more eagerly.

Original pull request: #83.
This commit is contained in:
Thomas Darimont
2014-06-18 12:52:26 +02:00
parent 42ff3ee1e1
commit 5f48ea8ed4
7 changed files with 235 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
@@ -36,10 +37,12 @@ import org.springframework.util.Assert;
* operations.
*
* @author Jennifer Hickey
* @author Thomas Darimont
*/
@SuppressWarnings("serial")
public class RedisAtomicDouble extends Number implements Serializable, BoundKeyOperations<String> {
private static final long serialVersionUID = 1L;
private volatile String key;
private ValueOperations<String, Double> operations;
private RedisOperations<String, Double> generalOps;
@@ -94,13 +97,17 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
*
* @param redisCounter the redis counter
* @param template the template
* @see #RedisAtomicDouble(String, RedisConnectionFactory, double)
*/
public RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> template) {
this(redisCounter, template, null);
}
/**
* Constructs a new <code>RedisAtomicDouble</code> instance.
* Constructs a new <code>RedisAtomicDouble</code> instance. Note: You need to configure the given {@code template}
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
* {@link #RedisAtomicDouble(String, RedisConnectionFactory, Double)} constructor which uses appropriate default
* serializers.
*
* @param redisCounter the redis counter
* @param template the template
@@ -111,8 +118,11 @@ public class RedisAtomicDouble extends Number implements Serializable, BoundKeyO
}
private RedisAtomicDouble(String redisCounter, RedisOperations<String, Double> template, Double initialValue) {
Assert.hasText(redisCounter, "a valid counter name is required");
Assert.notNull(template, "a valid template is required");
Assert.notNull(template.getKeySerializer(), "a valid key serializer in template is required");
Assert.notNull(template.getValueSerializer(), "a valid value serializer in template is required");
this.key = redisCounter;
this.generalOps = template;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,7 +28,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
/**
* Atomic integer backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
@@ -36,9 +38,12 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
*
* @see java.util.concurrent.atomic.AtomicInteger
* @author Costin Leau
* @author Thomas Darimont
*/
public class RedisAtomicInteger extends Number implements Serializable, BoundKeyOperations<String> {
private static final long serialVersionUID = 1L;
private volatile String key;
private ValueOperations<String, Integer> operations;
private RedisOperations<String, Integer> generalOps;
@@ -69,13 +74,17 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
*
* @param redisCounter the redis counter
* @param template the template
* @see #RedisAtomicInteger(String, RedisConnectionFactory, int)
*/
public RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer> template) {
this(redisCounter, template, null);
}
/**
* Constructs a new <code>RedisAtomicInteger</code> instance.
* Constructs a new <code>RedisAtomicInteger</code> instance. Note: You need to configure the given {@code template}
* with appropriate {@link RedisSerializer} for the key and value. As an alternative one could use the
* {@link #RedisAtomicInteger(String, RedisConnectionFactory, Integer)} constructor which uses appropriate default
* serializers.
*
* @param redisCounter the redis counter
* @param template the template
@@ -107,6 +116,12 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
}
private RedisAtomicInteger(String redisCounter, RedisOperations<String, Integer> template, Integer initialValue) {
Assert.hasText(redisCounter, "a valid counter name is required");
Assert.notNull(template, "a valid template is required");
Assert.notNull(template.getKeySerializer(), "a valid key serializer in template is required");
Assert.notNull(template.getValueSerializer(), "a valid value serializer in template is required");
this.key = redisCounter;
this.generalOps = template;
this.operations = generalOps.opsForValue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
@@ -37,9 +38,12 @@ import org.springframework.util.Assert;
*
* @see java.util.concurrent.atomic.AtomicLong
* @author Costin Leau
* @author Thomas Darimont
*/
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {
private static final long serialVersionUID = 1L;
private volatile String key;
private ValueOperations<String, Long> operations;
private RedisOperations<String, Long> generalOps;
@@ -94,6 +98,7 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
*
* @param redisCounter the redis counter
* @param template the template
* @see #RedisAtomicLong(String, RedisConnectionFactory, long)
*/
public RedisAtomicLong(String redisCounter, RedisOperations<String, Long> template) {
this(redisCounter, template, null);
@@ -101,6 +106,14 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
/**
* Constructs a new <code>RedisAtomicLong</code> instance.
* <p>
* Note: You need to configure the given {@code template} with appropriate {@link RedisSerializer} for the key and
* value. The key serializer must be able to deserialize to a {@link String} and the value serializer must be able to
* deserialize to a {@link Long}.
* <p>
* As an alternative one could use the {@link #RedisAtomicLong(String, RedisConnectionFactory, Long)} constructor
* which uses appropriate default serializers, in this case {@link StringRedisSerializer} for the key and
* {@link GenericToStringSerializer} for the value.
*
* @param redisCounter the redis counter
* @param template the template
@@ -111,8 +124,11 @@ public class RedisAtomicLong extends Number implements Serializable, BoundKeyOpe
}
private RedisAtomicLong(String redisCounter, RedisOperations<String, Long> template, Long initialValue) {
Assert.hasText(redisCounter, "a valid counter name is required");
Assert.notNull(template, "a valid template is required");
Assert.notNull(template.getKeySerializer(), "a valid key serializer in template is required");
Assert.notNull(template.getValueSerializer(), "a valid value serializer in template is required");
this.key = redisCounter;
this.generalOps = template;