mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
refactor(sequence): 优化Redis序列号生成器实现
- 使用Lombok的@Getter/@Setter简化stringRedisTemplate和stepStart字段访问 - 移除冗余的空字符串检查方法isEmpty - 清理未使用的getter/setter方法减少代码量 - 保持原有核心序列号生成逻辑不变
This commit is contained in:
1 parent
70855f8b5b
commit
a60127323e
1 file changed
+44
-55
+44
-55
@@ -3,6 +3,8 @@ package com.pig4cloud.pigx.common.sequence.range.impl.redis;
|
||||
import com.pig4cloud.pigx.common.sequence.exception.SeqException;
|
||||
import com.pig4cloud.pigx.common.sequence.range.SeqRange;
|
||||
import com.pig4cloud.pigx.common.sequence.range.SeqRangeMgr;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
@@ -12,69 +14,56 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
*/
|
||||
public class RedisSeqRangeMgr implements SeqRangeMgr {
|
||||
|
||||
/**
|
||||
* 前缀防止key重复
|
||||
*/
|
||||
private final static String KEY_PREFIX = "x_sequence_";
|
||||
/**
|
||||
* 前缀防止key重复
|
||||
*/
|
||||
private final static String KEY_PREFIX = "x_sequence_";
|
||||
|
||||
/**
|
||||
* redis客户端
|
||||
*/
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
/**
|
||||
* redis客户端
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 区间步长
|
||||
*/
|
||||
private int step = 1000;
|
||||
/**
|
||||
* 区间步长
|
||||
*/
|
||||
private int step = 1000;
|
||||
|
||||
/**
|
||||
* 区间起始位置,真实从stepStart+1开始
|
||||
*/
|
||||
private long stepStart = 0;
|
||||
/**
|
||||
* 区间起始位置,真实从stepStart+1开始
|
||||
*/
|
||||
@Setter
|
||||
private long stepStart = 0;
|
||||
|
||||
/**
|
||||
* 标记业务key是否存在,如果false,在取nextRange时,会取check一把 这个boolean只为提高性能,不用每次都取redis check
|
||||
*/
|
||||
private volatile boolean keyAlreadyExist;
|
||||
/**
|
||||
* 标记业务key是否存在,如果false,在取nextRange时,会取check一把 这个boolean只为提高性能,不用每次都取redis check
|
||||
*/
|
||||
private volatile boolean keyAlreadyExist;
|
||||
|
||||
@Override
|
||||
public SeqRange nextRange(String name) throws SeqException {
|
||||
if (!keyAlreadyExist) {
|
||||
@Override
|
||||
public SeqRange nextRange(String name) throws SeqException {
|
||||
if (!keyAlreadyExist) {
|
||||
|
||||
if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(getRealKey(name)))) {
|
||||
// 第一次不存在,进行初始化,setnx不存在就set,存在就忽略
|
||||
stringRedisTemplate.opsForValue().setIfAbsent(getRealKey(name), String.valueOf(stepStart));
|
||||
}
|
||||
keyAlreadyExist = true;
|
||||
}
|
||||
if (!stringRedisTemplate.hasKey(getRealKey(name))) {
|
||||
// 第一次不存在,进行初始化,setnx不存在就set,存在就忽略
|
||||
stringRedisTemplate.opsForValue().setIfAbsent(getRealKey(name), String.valueOf(stepStart));
|
||||
}
|
||||
keyAlreadyExist = true;
|
||||
}
|
||||
|
||||
Long max = stringRedisTemplate.opsForValue().increment(getRealKey(name), step);
|
||||
Long min = max - step + 1;
|
||||
return new SeqRange(min, max);
|
||||
}
|
||||
Long max = stringRedisTemplate.opsForValue().increment(getRealKey(name), step);
|
||||
Long min = max - step + 1;
|
||||
return new SeqRange(min, max);
|
||||
}
|
||||
|
||||
private String getRealKey(String name) {
|
||||
return KEY_PREFIX + name;
|
||||
}
|
||||
|
||||
private String getRealKey(String name) {
|
||||
return KEY_PREFIX + name;
|
||||
}
|
||||
private boolean isEmpty(String str) {
|
||||
return null == str || str.trim().isEmpty();
|
||||
}
|
||||
|
||||
private boolean isEmpty(String str) {
|
||||
return null == str || str.trim().length() == 0;
|
||||
}
|
||||
|
||||
public long getStepStart() {
|
||||
return stepStart;
|
||||
}
|
||||
|
||||
public void setStepStart(long stepStart) {
|
||||
this.stepStart = stepStart;
|
||||
}
|
||||
|
||||
public StringRedisTemplate getStringRedisTemplate() {
|
||||
return stringRedisTemplate;
|
||||
}
|
||||
|
||||
public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user