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