+ improve getAndXXX operations (taking advantage of the increment operation which is already atomic).
This commit is contained in:
@@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.support.atomic;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.keyvalue.redis.core.KeyBound;
|
||||
@@ -172,18 +171,11 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
|
||||
|
||||
/**
|
||||
* Atomically increment by one the current value.
|
||||
*
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndIncrement() {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
int value = get();
|
||||
generalOps.multi();
|
||||
operations.increment(key, 1);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return incrementAndGet() - 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -192,15 +184,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndDecrement() {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
int value = get();
|
||||
generalOps.multi();
|
||||
operations.increment(key, -1);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return decrementAndGet() + 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -210,15 +194,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
|
||||
* @return the previous value
|
||||
*/
|
||||
public int getAndAdd(final int delta) {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Integer>() {
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
int value = get();
|
||||
generalOps.multi();
|
||||
set(value + delta);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return addAndGet(delta) - delta;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.redis.support.atomic;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.keyvalue.redis.core.KeyBound;
|
||||
@@ -177,15 +176,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndIncrement() {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Long>() {
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
long value = get();
|
||||
generalOps.multi();
|
||||
operations.increment(key, 1);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return incrementAndGet() - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,15 +185,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndDecrement() {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Long>() {
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
long value = get();
|
||||
generalOps.multi();
|
||||
operations.increment(key, -11);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return decrementAndGet() + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,15 +195,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
|
||||
* @return the previous value
|
||||
*/
|
||||
public long getAndAdd(final long delta) {
|
||||
return CASUtils.execute(generalOps, key, new Callable<Long>() {
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
long value = get();
|
||||
generalOps.multi();
|
||||
set(value + delta);
|
||||
return value;
|
||||
}
|
||||
});
|
||||
return addAndGet(delta) - delta;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user