|
@@ -8,10 +8,13 @@ import lombok.Data;
|
|
|
import org.redisson.api.RMap;
|
|
import org.redisson.api.RMap;
|
|
|
import org.redisson.api.RedissonClient;
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.redisson.client.codec.IntegerCodec;
|
|
import org.redisson.client.codec.IntegerCodec;
|
|
|
|
|
+import org.springframework.cache.Cache;
|
|
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @ClassName : DefaultSerialStrategyClient
|
|
* @ClassName : DefaultSerialStrategyClient
|
|
@@ -26,31 +29,34 @@ public class DefaultNumberingStrategyClient implements NumberingStrategyClient {
|
|
|
*/
|
|
*/
|
|
|
private static final String CURRENT_NUM="currentNum";
|
|
private static final String CURRENT_NUM="currentNum";
|
|
|
private NumberingStrategyProperties properties;
|
|
private NumberingStrategyProperties properties;
|
|
|
- private RMap<String, Integer> operation;
|
|
|
|
|
private NumberingStrategyEnum strategyEnum;
|
|
private NumberingStrategyEnum strategyEnum;
|
|
|
|
|
+ private Cache cache;
|
|
|
|
|
|
|
|
- DefaultNumberingStrategyClient(NumberingStrategyProperties properties, RedissonClient redissonClient) {
|
|
|
|
|
- operation = redissonClient.getMap("global:serialStrategy:"+properties.getClientId(),new IntegerCodec());
|
|
|
|
|
|
|
+ DefaultNumberingStrategyClient(NumberingStrategyProperties properties,CacheManager cacheManager) {
|
|
|
|
|
+ Cache cache = cacheManager.getCache("serialStrategy" + properties.getClientId());
|
|
|
|
|
+// operation = redissonClient.getMap("global:serialStrategy:"+properties.getClientId(),new IntegerCodec());
|
|
|
doInit(properties);
|
|
doInit(properties);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public String getAndIncrementSerialNum() {
|
|
public String getAndIncrementSerialNum() {
|
|
|
- Integer nextNum = this.operation.addAndGet(CURRENT_NUM, 1);
|
|
|
|
|
|
|
+ Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
|
|
+ Integer nextNum=current+1;
|
|
|
|
|
+ cache.put(CURRENT_NUM,nextNum);
|
|
|
return formatSerialNo(nextNum);
|
|
return formatSerialNo(nextNum);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public String getNextFormatNum() {
|
|
public String getNextFormatNum() {
|
|
|
- Integer currentNum = this.operation.get(CURRENT_NUM);
|
|
|
|
|
- Integer nextNum=currentNum+1;
|
|
|
|
|
|
|
+ Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
|
|
+ Integer nextNum=current+1;
|
|
|
return formatSerialNo(nextNum);
|
|
return formatSerialNo(nextNum);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Integer getNextNum() {
|
|
public Integer getNextNum() {
|
|
|
- Integer currentNum = this.operation.get(CURRENT_NUM);
|
|
|
|
|
- return currentNum+1;
|
|
|
|
|
|
|
+ Integer current = Optional.ofNullable(cache.get(CURRENT_NUM, Integer.class)).orElse(0);
|
|
|
|
|
+ return current+1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -99,13 +105,13 @@ public class DefaultNumberingStrategyClient implements NumberingStrategyClient {
|
|
|
Integer nextNum = properties.getNextNum();
|
|
Integer nextNum = properties.getNextNum();
|
|
|
if (Objects.isNull(nextNum)) {
|
|
if (Objects.isNull(nextNum)) {
|
|
|
//更新值为null,缓存值不为null,则不进行更新操作
|
|
//更新值为null,缓存值不为null,则不进行更新操作
|
|
|
- nextNum = this.operation.get(CURRENT_NUM);
|
|
|
|
|
|
|
+ nextNum = cache.get(CURRENT_NUM, Integer.class);
|
|
|
if(nextNum!=null){
|
|
if(nextNum!=null){
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
//当nextNum为空或者小于0时,将currentNum设置为0
|
|
//当nextNum为空或者小于0时,将currentNum设置为0
|
|
|
Integer currentNum=( Objects.isNull(nextNum)||nextNum<=0)?0:nextNum-1;
|
|
Integer currentNum=( Objects.isNull(nextNum)||nextNum<=0)?0:nextNum-1;
|
|
|
- this.operation.put(CURRENT_NUM,currentNum);
|
|
|
|
|
|
|
+ cache.put(CURRENT_NUM,currentNum);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|