Skip to content

Commit 5444667

Browse files
DATAREDIS-481 - Some code cleanups, JavaDoc, Builders and tests.
1 parent 833630f commit 5444667

File tree

10 files changed

+539
-82
lines changed

10 files changed

+539
-82
lines changed

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import org.springframework.util.Assert;
2929

3030
/**
31+
* {@link RedisCacheWriter} implementation capable of reading/writing binary data from/to Redis in {@literal standalone}
32+
* and {@literal cluster} environments. Works upon a given {@link RedisConnectionFactory} to obtain the actual
33+
* {@link RedisConnection}.
34+
*
3135
* @author Christoph Strobl
3236
* @since 2.0
3337
*/
@@ -61,10 +65,14 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
6165
}
6266

6367
public static DefaultRedisCacheWriter nonLockingRedisCacheWriter(RedisConnectionFactory connectionFactory) {
68+
69+
Assert.notNull(connectionFactory, "ConnectionFactory must not be null!");
6470
return new DefaultRedisCacheWriter(connectionFactory);
6571
}
6672

6773
public static DefaultRedisCacheWriter lockingRedisCacheWriter(RedisConnectionFactory connectionFactory) {
74+
75+
Assert.notNull(connectionFactory, "ConnectionFactory must not be null!");
6876
return new DefaultRedisCacheWriter(connectionFactory, Duration.ofMillis(50));
6977
}
7078

@@ -128,6 +136,11 @@ public void remove(String name, byte[] key) {
128136
execute(name, connection -> connection.del(key));
129137
}
130138

139+
/**
140+
* Explicitly set a write lock on a cache.
141+
*
142+
* @param name the name of the cache to lock.
143+
*/
131144
public void lock(String name) {
132145
execute(name, connection -> doLock(name, connection));
133146
}
@@ -136,6 +149,11 @@ private Boolean doLock(String name, RedisConnection connection) {
136149
return connection.setNX(createCacheLockKey(name), new byte[] {});
137150
}
138151

152+
/**
153+
* Explicitly remove a write lock from a cache.
154+
*
155+
* @param name the name of the cache to unlock.
156+
*/
139157
public void unlock(String name) {
140158
executeWithoutLockCheck(connection -> doUnlock(name, connection));
141159
}
@@ -144,6 +162,12 @@ private Long doUnlock(String name, RedisConnection connection) {
144162
return connection.del(createCacheLockKey(name));
145163
}
146164

165+
/**
166+
* Check if a cache has set a lock.
167+
*
168+
* @param name the name of the cache to check for presence of a lock.
169+
* @return {@literal true} if lock found.
170+
*/
147171
public boolean isLoked(String name) {
148172
return executeWithoutLockCheck(connection -> doCheckLock(name, connection));
149173
}
@@ -183,7 +207,14 @@ public void clean(String name, byte[] pattern) {
183207
});
184208
}
185209

186-
public <T> T execute(String name, ConnectionCallback<T> callback) {
210+
/**
211+
* @return {@literal true} if {@link RedisCacheWriter} uses locks.
212+
*/
213+
public boolean isLockingCacheWriter() {
214+
return !sleepTime.isZero() && !sleepTime.isNegative();
215+
}
216+
217+
<T> T execute(String name, ConnectionCallback<T> callback) {
187218

188219
RedisConnection connection = connectionFactory.getConnection();
189220
try {
@@ -206,10 +237,6 @@ private <T> T executeWithoutLockCheck(ConnectionCallback<T> callback) {
206237
}
207238
}
208239

209-
public boolean isLockingCacheWriter() {
210-
return !sleepTime.isZero() && !sleepTime.isNegative();
211-
}
212-
213240
private void checkAndPotentiallyWaitUntilUnlocked(String name, RedisConnection connection) {
214241

215242
if (isLockingCacheWriter()) {
@@ -234,6 +261,11 @@ byte[] createCacheLockKey(String name) {
234261
return (name + "~lock").getBytes(Charset.forName("UTF-8"));
235262
}
236263

264+
/**
265+
* @author Christoph Strobl
266+
* @param <T>
267+
* @since 2.0
268+
*/
237269
interface ConnectionCallback<T> {
238270
T doWithConnection(RedisConnection connection);
239271
}

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
import org.springframework.core.convert.support.ConfigurableConversionService;
2727
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
2828
import org.springframework.format.support.DefaultFormattingConversionService;
29+
import org.springframework.lang.Nullable;
2930
import org.springframework.util.Assert;
3031
import org.springframework.util.ObjectUtils;
3132
import org.springframework.util.ReflectionUtils;
3233

3334
/**
34-
* {@link org.springframework.cache.Cache} implementation suitable for Redis.
35+
* {@link org.springframework.cache.Cache} implementation using for Redis as underlying store.
3536
*
3637
* @author Christoph Strobl
3738
* @since 2.0
@@ -74,7 +75,7 @@ public NRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfigur
7475
this.cacheWriter = cacheWriter;
7576
this.cacheConfig = cacheConfig;
7677

77-
conversionService.addConverter(String.class, byte[].class, source -> source.getBytes(Charset.forName("UTF8")));
78+
conversionService.addConverter(String.class, byte[].class, source -> source.getBytes(Charset.forName("UTF-8")));
7879
}
7980

8081
@Override
@@ -125,7 +126,7 @@ public void put(Object key, Object value) {
125126
name));
126127
}
127128

128-
cacheWriter.put(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), cacheConfig.getTimeout());
129+
cacheWriter.put(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), cacheConfig.getTtl());
129130
}
130131

131132
@Override
@@ -138,7 +139,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
138139
}
139140

140141
byte[] result = cacheWriter.putIfAbsent(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue),
141-
cacheConfig.getTimeout());
142+
cacheConfig.getTtl());
142143

143144
if (result == null) {
144145
return null;
@@ -159,7 +160,24 @@ public void clear() {
159160
cacheWriter.clean(name, pattern);
160161
}
161162

162-
protected Object preProcessCacheValue(Object value) {
163+
/**
164+
* Get {@link RedisCacheConfiguration} used.
165+
*
166+
* @return immutable {@link RedisCacheConfiguration}. Never {@literal null}.
167+
*/
168+
public RedisCacheConfiguration getCacheConfiguration() {
169+
return cacheConfig;
170+
}
171+
172+
/**
173+
* Customization hook called before passing object to
174+
* {@link org.springframework.data.redis.serializer.RedisSerializer}.
175+
*
176+
* @param value can be {@literal null}.
177+
* @return preprocessed value. Can be {@literal null}.
178+
*/
179+
@Nullable
180+
protected Object preProcessCacheValue(@Nullable Object value) {
163181

164182
if (value != null) {
165183
return value;
@@ -168,10 +186,22 @@ protected Object preProcessCacheValue(Object value) {
168186
return isAllowNullValues() ? NULL_VALUE : null;
169187
}
170188

189+
/**
190+
* Serialize the key.
191+
*
192+
* @param cacheKey must not be {@literal null}.
193+
* @return never {@literal null}.
194+
*/
171195
protected byte[] serializeCacheKey(String cacheKey) {
172196
return cacheConfig.getKeySerializationPair().write(cacheKey).array();
173197
}
174198

199+
/**
200+
* Serialize the value to cache.
201+
*
202+
* @param value must not be {@literal null}.
203+
* @return never {@literal null}.
204+
*/
175205
protected byte[] serializeCacheValue(Object value) {
176206

177207
if (isAllowNullValues() && value instanceof NullValue) {
@@ -181,6 +211,13 @@ protected byte[] serializeCacheValue(Object value) {
181211
return cacheConfig.getValueSerializationPair().write(value).array();
182212
}
183213

214+
/**
215+
* Deserialize the given value to the actual cache value.
216+
*
217+
* @param value must not be {@literal null}.
218+
* @return can be {@literal null}.
219+
*/
220+
@Nullable
184221
protected Object deserializeCacheValue(byte[] value) {
185222

186223
if (isAllowNullValues() && ObjectUtils.nullSafeEquals(value, BINARY_NULL_VALUE)) {
@@ -205,7 +242,7 @@ private String createCacheKey(Object key) {
205242
}
206243

207244
private String prefixCacheKey(String key) {
208-
return cacheConfig.getDefaultPrefix().orElseGet(() -> name + "::") + key;
245+
return cacheConfig.getKeyPrefix().orElseGet(() -> name + "::") + key;
209246
}
210247

211248
private <T> T valueFromLoader(Object key, Callable<T> valueLoader) {

0 commit comments

Comments
 (0)