Skip to content

Commit 863e5b8

Browse files
committed
DATAREDIS-553 - Polishing.
Improve javadoc. Replace static method imports with qualified method calls. Refactor duplicate RedisCacheKey construction into own method. Fix formatting. Original pull request: spring-projects#221.
1 parent f26441d commit 863e5b8

File tree

2 files changed

+55
-59
lines changed

2 files changed

+55
-59
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCache.java

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.redis.cache;
1817

1918
import static org.springframework.util.Assert.*;
@@ -40,6 +39,7 @@
4039
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
4140
import org.springframework.data.redis.serializer.RedisSerializer;
4241
import org.springframework.data.redis.serializer.StringRedisSerializer;
42+
import org.springframework.util.Assert;
4343
import org.springframework.util.ClassUtils;
4444
import org.springframework.util.ObjectUtils;
4545

@@ -54,13 +54,13 @@
5454
@SuppressWarnings("unchecked")
5555
public class RedisCache extends AbstractValueAdaptingCache {
5656

57-
@SuppressWarnings("rawtypes")//
57+
@SuppressWarnings("rawtypes") //
5858
private final RedisOperations redisOperations;
5959
private final RedisCacheMetadata cacheMetadata;
6060
private final CacheValueAccessor cacheValueAccessor;
6161

6262
/**
63-
* Constructs a new <code>RedisCache</code> instance.
63+
* Constructs a new {@link RedisCache} instance.
6464
*
6565
* @param name cache name
6666
* @param prefix
@@ -73,10 +73,10 @@ public RedisCache(String name, byte[] prefix, RedisOperations<? extends Object,
7373
}
7474

7575
/**
76-
* Constructs a new <code>RedisCache</code> instance.
76+
* Constructs a new {@link RedisCache} instance.
7777
*
7878
* @param name cache name
79-
* @param prefix
79+
* @param prefix must not be {@literal null} or empty.
8080
* @param redisOperations
8181
* @param expiration
8282
* @param allowNullValues
@@ -87,15 +87,14 @@ public RedisCache(String name, byte[] prefix, RedisOperations<? extends Object,
8787

8888
super(allowNullValues);
8989

90-
hasText(name, "non-empty cache name is required");
91-
this.cacheMetadata = new RedisCacheMetadata(name, prefix);
92-
this.cacheMetadata.setDefaultExpiration(expiration);
93-
94-
this.redisOperations = redisOperations;
90+
Assert.hasText(name, "CacheName must not be null or empty!");
9591

9692
RedisSerializer<?> serializer = redisOperations.getValueSerializer() != null ? redisOperations.getValueSerializer()
9793
: (RedisSerializer<?>) new JdkSerializationRedisSerializer();
9894

95+
this.cacheMetadata = new RedisCacheMetadata(name, prefix);
96+
this.cacheMetadata.setDefaultExpiration(expiration);
97+
this.redisOperations = redisOperations;
9998
this.cacheValueAccessor = new CacheValueAccessor(serializer);
10099

101100
if (allowNullValues) {
@@ -105,7 +104,9 @@ public RedisCache(String name, byte[] prefix, RedisOperations<? extends Object,
105104
|| redisOperations.getValueSerializer() instanceof JacksonJsonRedisSerializer
106105
|| redisOperations.getValueSerializer() instanceof Jackson2JsonRedisSerializer) {
107106
throw new IllegalArgumentException(String.format(
108-
"Redis does not allow keys with null value ¯\\_(ツ)_/¯. The chosen %s does not support generic type handling and therefore cannot be used with allowNullValues enabled. Please use a different RedisSerializer or disable null value support.",
107+
"Redis does not allow keys with null value ¯\\_(ツ)_/¯. "
108+
+ "The chosen %s does not support generic type handling and therefore cannot be used with allowNullValues enabled. "
109+
+ "Please use a different RedisSerializer or disable null value support.",
109110
ClassUtils.getShortName(redisOperations.getValueSerializer().getClass())));
110111
}
111112
}
@@ -132,8 +133,7 @@ public <T> T get(Object key, Class<T> type) {
132133
*/
133134
@Override
134135
public ValueWrapper get(Object key) {
135-
return get(new RedisCacheKey(key).usePrefix(this.cacheMetadata.getKeyPrefix()).withKeySerializer(
136-
redisOperations.getKeySerializer()));
136+
return get(getRedisCacheKey(key));
137137
}
138138

139139
/*
@@ -143,9 +143,7 @@ public ValueWrapper get(Object key) {
143143
public <T> T get(final Object key, final Callable<T> valueLoader) {
144144

145145
BinaryRedisCacheElement rce = new BinaryRedisCacheElement(
146-
new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix())
147-
.withKeySerializer(redisOperations.getKeySerializer()), new StoreTranslatingCallable(valueLoader)),
148-
cacheValueAccessor);
146+
new RedisCacheElement(getRedisCacheKey(key), new StoreTranslatingCallable(valueLoader)), cacheValueAccessor);
149147

150148
ValueWrapper val = get(key);
151149
if (val != null) {
@@ -171,7 +169,7 @@ public <T> T get(final Object key, final Callable<T> valueLoader) {
171169
*/
172170
public RedisCacheElement get(final RedisCacheKey cacheKey) {
173171

174-
notNull(cacheKey, "CacheKey must not be null!");
172+
Assert.notNull(cacheKey, "CacheKey must not be null!");
175173

176174
Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {
177175

@@ -195,9 +193,8 @@ public Boolean doInRedis(RedisConnection connection) throws DataAccessException
195193
@Override
196194
public void put(final Object key, final Object value) {
197195

198-
put(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix())
199-
.withKeySerializer(redisOperations.getKeySerializer()), toStoreValue(value))
200-
.expireAfter(cacheMetadata.getDefaultExpiration()));
196+
put(new RedisCacheElement(getRedisCacheKey(key), toStoreValue(value))
197+
.expireAfter(cacheMetadata.getDefaultExpiration()));
201198
}
202199

203200
/*
@@ -225,10 +222,10 @@ protected Object fromStoreValue(Object storeValue) {
225222
*/
226223
public void put(RedisCacheElement element) {
227224

228-
notNull(element, "Element must not be null!");
225+
Assert.notNull(element, "Element must not be null!");
229226

230-
redisOperations.execute(new RedisCachePutCallback(new BinaryRedisCacheElement(element, cacheValueAccessor),
231-
cacheMetadata));
227+
redisOperations
228+
.execute(new RedisCachePutCallback(new BinaryRedisCacheElement(element, cacheValueAccessor), cacheMetadata));
232229
}
233230

234231
/*
@@ -237,9 +234,8 @@ public void put(RedisCacheElement element) {
237234
*/
238235
public ValueWrapper putIfAbsent(Object key, final Object value) {
239236

240-
return putIfAbsent(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix())
241-
.withKeySerializer(redisOperations.getKeySerializer()), toStoreValue(value))
242-
.expireAfter(cacheMetadata.getDefaultExpiration()));
237+
return putIfAbsent(new RedisCacheElement(getRedisCacheKey(key), toStoreValue(value))
238+
.expireAfter(cacheMetadata.getDefaultExpiration()));
243239
}
244240

245241
/**
@@ -252,22 +248,20 @@ public ValueWrapper putIfAbsent(Object key, final Object value) {
252248
*/
253249
public ValueWrapper putIfAbsent(RedisCacheElement element) {
254250

255-
notNull(element, "Element must not be null!");
251+
Assert.notNull(element, "Element must not be null!");
256252

257253
new RedisCachePutIfAbsentCallback(new BinaryRedisCacheElement(element, cacheValueAccessor), cacheMetadata);
258254

259-
return toWrapper(cacheValueAccessor.deserializeIfNecessary((byte[]) redisOperations
260-
.execute(new RedisCachePutIfAbsentCallback(new BinaryRedisCacheElement(element, cacheValueAccessor),
261-
cacheMetadata))));
255+
return toWrapper(cacheValueAccessor.deserializeIfNecessary((byte[]) redisOperations.execute(
256+
new RedisCachePutIfAbsentCallback(new BinaryRedisCacheElement(element, cacheValueAccessor), cacheMetadata))));
262257
}
263258

264259
/*
265260
* (non-Javadoc)
266261
* @see org.springframework.cache.Cache#evict(java.lang.Object)
267262
*/
268263
public void evict(Object key) {
269-
evict(new RedisCacheElement(new RedisCacheKey(key).usePrefix(cacheMetadata.getKeyPrefix()).withKeySerializer(
270-
redisOperations.getKeySerializer()), null));
264+
evict(new RedisCacheElement(getRedisCacheKey(key), null));
271265
}
272266

273267
/**
@@ -276,9 +270,9 @@ public void evict(Object key) {
276270
*/
277271
public void evict(final RedisCacheElement element) {
278272

279-
notNull(element, "Element must not be null!");
280-
redisOperations.execute(new RedisCacheEvictCallback(new BinaryRedisCacheElement(element, cacheValueAccessor),
281-
cacheMetadata));
273+
Assert.notNull(element, "Element must not be null!");
274+
redisOperations
275+
.execute(new RedisCacheEvictCallback(new BinaryRedisCacheElement(element, cacheValueAccessor), cacheMetadata));
282276
}
283277

284278
/*
@@ -317,9 +311,7 @@ private ValueWrapper toWrapper(Object value) {
317311
@Override
318312
protected Object lookup(Object key) {
319313

320-
RedisCacheKey cacheKey = key instanceof RedisCacheKey ? (RedisCacheKey) key
321-
: new RedisCacheKey(key).usePrefix(this.cacheMetadata.getKeyPrefix())
322-
.withKeySerializer(redisOperations.getKeySerializer());
314+
RedisCacheKey cacheKey = key instanceof RedisCacheKey ? (RedisCacheKey) key : getRedisCacheKey(key);
323315

324316
byte[] bytes = (byte[]) redisOperations.execute(new AbstractRedisCacheCallback<byte[]>(
325317
new BinaryRedisCacheElement(new RedisCacheElement(cacheKey, null), cacheValueAccessor), cacheMetadata) {
@@ -333,6 +325,11 @@ public byte[] doInRedis(BinaryRedisCacheElement element, RedisConnection connect
333325
return bytes == null ? null : cacheValueAccessor.deserializeIfNecessary(bytes);
334326
}
335327

328+
private RedisCacheKey getRedisCacheKey(Object key) {
329+
return new RedisCacheKey(key).usePrefix(this.cacheMetadata.getKeyPrefix())
330+
.withKeySerializer(redisOperations.getKeySerializer());
331+
}
332+
336333
/**
337334
* {@link Callable} to transform a value obtained from another {@link Callable} to its store value.
338335
*
@@ -375,7 +372,7 @@ static class RedisCacheMetadata {
375372
*/
376373
public RedisCacheMetadata(String cacheName, byte[] keyPrefix) {
377374

378-
hasText(cacheName, "CacheName must not be null or empty!");
375+
Assert.hasText(cacheName, "CacheName must not be null or empty!");
379376
this.cacheName = cacheName;
380377
this.keyPrefix = keyPrefix;
381378

@@ -431,8 +428,8 @@ public String getCacheName() {
431428

432429
/**
433430
* Set the default expiration time in seconds
434-
*
435-
* @param defaultExpiration
431+
*
432+
* @param seconds
436433
*/
437434
public void setDefaultExpiration(long seconds) {
438435
this.defaultExpiration = seconds;
@@ -455,7 +452,7 @@ public long getDefaultExpiration() {
455452
*/
456453
static class CacheValueAccessor {
457454

458-
@SuppressWarnings("rawtypes")//
455+
@SuppressWarnings("rawtypes") //
459456
private final RedisSerializer valueSerializer;
460457

461458
@SuppressWarnings("rawtypes")
@@ -687,8 +684,8 @@ public Void doInLock(RedisConnection connection) {
687684

688685
do {
689686
// need to paginate the keys
690-
Set<byte[]> keys = connection.zRange(metadata.getSetOfKnownKeysKey(), (offset) * PAGE_SIZE, (offset + 1)
691-
* PAGE_SIZE - 1);
687+
Set<byte[]> keys = connection.zRange(metadata.getSetOfKnownKeysKey(), (offset) * PAGE_SIZE,
688+
(offset + 1) * PAGE_SIZE - 1);
692689
finished = keys.size() < PAGE_SIZE;
693690
offset++;
694691
if (!keys.isEmpty()) {
@@ -707,8 +704,8 @@ public Void doInLock(RedisConnection connection) {
707704
*/
708705
static class RedisCacheCleanByPrefixCallback extends LockingRedisCacheCallback<Void> {
709706

710-
private static final byte[] REMOVE_KEYS_BY_PATTERN_LUA = new StringRedisSerializer()
711-
.serialize("local keys = redis.call('KEYS', ARGV[1]); local keysCount = table.getn(keys); if(keysCount > 0) then for _, key in ipairs(keys) do redis.call('del', key); end; end; return keysCount;");
707+
private static final byte[] REMOVE_KEYS_BY_PATTERN_LUA = new StringRedisSerializer().serialize(
708+
"local keys = redis.call('KEYS', ARGV[1]); local keysCount = table.getn(keys); if(keysCount > 0) then for _, key in ipairs(keys) do redis.call('del', key); end; end; return keysCount;");
712709
private static final byte[] WILD_CARD = new StringRedisSerializer().serialize("*");
713710
private final RedisCacheMetadata metadata;
714711

@@ -727,7 +724,7 @@ public Void doInLock(RedisConnection connection) throws DataAccessException {
727724
byte[] prefixToUse = Arrays.copyOf(metadata.getKeyPrefix(), metadata.getKeyPrefix().length + WILD_CARD.length);
728725
System.arraycopy(WILD_CARD, 0, prefixToUse, metadata.getKeyPrefix().length, WILD_CARD.length);
729726

730-
if(isClusterConnection(connection)) {
727+
if (isClusterConnection(connection)) {
731728

732729
// load keys to the client because currently Redis Cluster connections do not allow eval of lua scripts.
733730
Set<byte[]> keys = connection.keys(prefixToUse);
@@ -913,8 +910,8 @@ public RuntimeException create(Object key, Callable<?> valueLoader, Throwable ca
913910

914911
if (isSpring43) {
915912
try {
916-
Class<?> execption = ClassUtils.forName("org.springframework.cache.Cache$ValueRetrievalException", this
917-
.getClass().getClassLoader());
913+
Class<?> execption = ClassUtils.forName("org.springframework.cache.Cache$ValueRetrievalException",
914+
this.getClass().getClassLoader());
918915
Constructor<?> c = ClassUtils.getConstructorIfAvailable(execption, Object.class, Callable.class,
919916
Throwable.class);
920917
return (RuntimeException) c.newInstance(key, valueLoader, cause);
@@ -923,8 +920,8 @@ public RuntimeException create(Object key, Callable<?> valueLoader, Throwable ca
923920
}
924921
}
925922

926-
return new RedisSystemException(String.format("Value for key '%s' could not be loaded using '%s'.", key,
927-
valueLoader), cause);
923+
return new RedisSystemException(
924+
String.format("Value for key '%s' could not be loaded using '%s'.", key, valueLoader), cause);
928925
}
929926
}
930927

src/main/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializer.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public <T> T deserialize(byte[] source, Class<T> type) throws SerializationExcep
132132
}
133133

134134
/**
135-
* {@link StdSerializer} adding class information required by default typing. This allows de-/seriialization of
135+
* {@link StdSerializer} adding class information required by default typing. This allows de-/serialization of
136136
* {@link NullValue}.
137137
*
138138
* @author Christoph Strobl
@@ -141,15 +141,15 @@ public <T> T deserialize(byte[] source, Class<T> type) throws SerializationExcep
141141
private class NullValueSerializer extends StdSerializer<NullValue> {
142142

143143
private static final long serialVersionUID = 1999052150548658808L;
144-
private final String classIdentifyer;
144+
private final String classIdentifier;
145145

146146
/**
147-
* @param classIdentifyer can be {@literal null} and will be defaulted to {@code @class}.
147+
* @param classIdentifier can be {@literal null} and will be defaulted to {@code @class}.
148148
*/
149-
NullValueSerializer(String classIdentifyer) {
149+
NullValueSerializer(String classIdentifier) {
150150

151151
super(NullValue.class);
152-
this.classIdentifyer = StringUtils.hasText(classIdentifyer) ? classIdentifyer : "@class";
152+
this.classIdentifier = StringUtils.hasText(classIdentifier) ? classIdentifier : "@class";
153153
}
154154

155155
/*
@@ -158,12 +158,11 @@ private class NullValueSerializer extends StdSerializer<NullValue> {
158158
*/
159159
@Override
160160
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider)
161-
throws IOException, JsonProcessingException {
161+
throws IOException {
162162

163163
jgen.writeStartObject();
164-
jgen.writeStringField(classIdentifyer, NullValue.class.getName());
164+
jgen.writeStringField(classIdentifier, NullValue.class.getName());
165165
jgen.writeEndObject();
166166
}
167-
168167
}
169168
}

0 commit comments

Comments
 (0)