Skip to content

Commit

Permalink
GH-2884: RedisUtils improvements
Browse files Browse the repository at this point in the history
Fixes #2884

- synchronize the map
- only call once per component

# Conflicts:
#	spring-integration-redis/src/test/java/org/springframework/integration/redis/util/RedisLockRegistryTests.java
  • Loading branch information
garyrussell authored and artembilan committed Apr 5, 2019
1 parent 9144f6d commit bfebbbb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class RedisMessageStore extends AbstractKeyValueMessageStore implements B

private final RedisTemplate<Object, Object> redisTemplate;

private final boolean unlinkAvailable;

private boolean valueSerializerSet;

/**
Expand Down Expand Up @@ -72,6 +74,7 @@ public RedisMessageStore(RedisConnectionFactory connectionFactory, String prefix
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.afterPropertiesSet();
this.unlinkAvailable = RedisUtils.isUnlinkAvailable(this.redisTemplate);
}

@Override
Expand Down Expand Up @@ -131,7 +134,7 @@ protected Object doRemove(Object id) {
Assert.notNull(id, "'id' must not be null");
Object removedObject = this.doRetrieve(id);
if (removedObject != null) {
if (RedisUtils.isUnlinkAvailable(this.redisTemplate)) {
if (this.unlinkAvailable) {
this.redisTemplate.unlink(id);
}
else {
Expand All @@ -143,7 +146,7 @@ protected Object doRemove(Object id) {

@Override
protected void doRemoveAll(Collection<Object> ids) {
if (RedisUtils.isUnlinkAvailable(this.redisTemplate)) {
if (this.unlinkAvailable) {
this.redisTemplate.unlink(ids);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl

private final String registryKey;

private final boolean unlinkAvailable;

private final StringRedisTemplate redisTemplate;

private final RedisScript<Boolean> obtainLockScript;
Expand Down Expand Up @@ -138,6 +140,7 @@ public RedisLockRegistry(RedisConnectionFactory connectionFactory, String regist
this.obtainLockScript = new DefaultRedisScript<>(OBTAIN_LOCK_SCRIPT, Boolean.class);
this.registryKey = registryKey;
this.expireAfter = expireAfter;
this.unlinkAvailable = RedisUtils.isUnlinkAvailable(this.redisTemplate);
}

/**
Expand Down Expand Up @@ -184,6 +187,8 @@ private final class RedisLock implements Lock {

private final ReentrantLock localLock = new ReentrantLock();

private final boolean unlinkAvailable = RedisLockRegistry.this.unlinkAvailable;

private volatile long lockedAt;

private RedisLock(String path) {
Expand Down Expand Up @@ -329,7 +334,7 @@ public void unlock() {
}

private void removeLockKey() {
if (RedisUtils.isUnlinkAvailable(RedisLockRegistry.this.redisTemplate)) {
if (this.unlinkAvailable) {
RedisLockRegistry.this.redisTemplate.unlink(this.lockKey);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.integration.redis.util;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -41,14 +42,14 @@ public final class RedisUtils {

@SuppressWarnings("serial")
private static final Map<RedisOperations<?, ?>, Boolean> unlinkAvailable =
new LinkedHashMap<RedisOperations<?, ?>, Boolean>() {
Collections.synchronizedMap(new LinkedHashMap<RedisOperations<?, ?>, Boolean>() {

@Override
protected boolean removeEldestEntry(Entry<RedisOperations<?, ?>, Boolean> eldest) {
return size() > 100;
}

};
});

/**
* Perform an {@code INFO} command on the provided {@link RedisOperations} to check
Expand Down

0 comments on commit bfebbbb

Please sign in to comment.