+ extracted cas rename logic into utility class
This commit is contained in:
@@ -18,12 +18,9 @@ package org.springframework.data.keyvalue.redis.support.collections;
|
||||
import java.util.AbstractCollection;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
|
||||
/**
|
||||
* Base implementation for {@link RedisCollection}.
|
||||
@@ -144,56 +141,13 @@ public abstract class AbstractRedisCollection<E> extends AbstractCollection<E> i
|
||||
|
||||
@Override
|
||||
public void rename(final String newKey) {
|
||||
operations.execute(new SessionCallback<Object>() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Object execute(RedisOperations operations) throws DataAccessException {
|
||||
do {
|
||||
operations.watch(key);
|
||||
|
||||
if (operations.hasKey(key)) {
|
||||
operations.multi();
|
||||
operations.rename(key, newKey);
|
||||
}
|
||||
else {
|
||||
operations.multi();
|
||||
}
|
||||
} while (operations.exec() == null);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
CollectionUtils.rename(key, newKey, operations);
|
||||
key = newKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean renameIfAbsent(final String newKey) {
|
||||
Boolean result = operations.execute(new SessionCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean execute(RedisOperations operations) throws DataAccessException {
|
||||
List<Object> exec = null;
|
||||
do {
|
||||
operations.watch(key);
|
||||
|
||||
if (operations.hasKey(key)) {
|
||||
operations.multi();
|
||||
operations.renameIfAbsent(key, newKey);
|
||||
}
|
||||
else {
|
||||
operations.watch(newKey);
|
||||
operations.multi();
|
||||
operations.hasKey(newKey);
|
||||
operations.hasKey(newKey);
|
||||
}
|
||||
exec = operations.exec();
|
||||
} while (exec == null);
|
||||
|
||||
boolean result = ((Long) exec.get(0) == 1);
|
||||
if (exec.size()>1) {
|
||||
result = !result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
Boolean result = CollectionUtils.renameIfAbsent(key, newKey, operations);
|
||||
|
||||
if (Boolean.TRUE.equals(result)) {
|
||||
key = newKey;
|
||||
|
||||
@@ -20,6 +20,10 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
|
||||
/**
|
||||
* Utility class used mainly for type conversion by the default collection implementations.
|
||||
* Meant for internal use.
|
||||
@@ -48,4 +52,55 @@ abstract class CollectionUtils {
|
||||
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
||||
static <K> void rename(final K key, final K newKey, RedisOperations<K, ?> operations) {
|
||||
operations.execute(new SessionCallback<Object>() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Object execute(RedisOperations operations) throws DataAccessException {
|
||||
do {
|
||||
operations.watch(key);
|
||||
|
||||
if (operations.hasKey(key)) {
|
||||
operations.multi();
|
||||
operations.rename(key, newKey);
|
||||
}
|
||||
else {
|
||||
operations.multi();
|
||||
}
|
||||
} while (operations.exec() == null);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static <K> Boolean renameIfAbsent(final K key, final K newKey, RedisOperations<K, ?> operations) {
|
||||
return operations.execute(new SessionCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean execute(RedisOperations operations) throws DataAccessException {
|
||||
List<Object> exec = null;
|
||||
do {
|
||||
operations.watch(key);
|
||||
|
||||
if (operations.hasKey(key)) {
|
||||
operations.multi();
|
||||
operations.renameIfAbsent(key, newKey);
|
||||
}
|
||||
else {
|
||||
operations.watch(newKey);
|
||||
operations.multi();
|
||||
operations.hasKey(newKey);
|
||||
operations.hasKey(newKey);
|
||||
}
|
||||
exec = operations.exec();
|
||||
} while (exec == null);
|
||||
|
||||
boolean result = ((Long) exec.get(0) == 1);
|
||||
if (exec.size() > 1) {
|
||||
result = !result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user