思想:Id内存加锁+DB落库
菜鸡实现篇:
@Repository
public class IdMakerRepo extends Repo<IdMaker> {
static Lock lock = new ReentrantLock();
static Cache<Object, Object> localCahce = Caffeine.newBuilder().expireAfterAccess(1, TimeUnit.DAYS).build();
public String getId(String prefix) {
return _getId(prefix);
}
private String _getId(String prefix) {
lock.lock();
try {
String name = prefix == null ? "tool__idmaker" : prefix;
IdMaker idmaker = (IdMaker) localCahce.get(name, a -> {
KPairIndex query = new KPairIndex();
String s = $.date.toString(new Date(), "yyyy-MM-dd");
query.setName(name);
Date startDate = $.date.toDate(s, "yyyy-MM-dd");
query._gt.setLastmodifytime(startDate);
Date endDate = $.date.toDate(s, "yyyy-MM-dd");
query._lt.setLastmodifytime(DateUtils.addDays(endDate, 1));
IdMaker idMaker = null;
List<IdMaker> idMakers = this.get(1, 1, query);
if ($.list.isHasRow(idMakers)) {
idMaker = idMakers.get(0);
}
if (idMaker == null) {
StringBuilder sb = new StringBuilder();
sb.append(prefix);
sb.append($.date.toString(new Date(), "yyyyMMdd"));
sb.append("0001");
idMaker = new IdMaker(UUID.randomUUID().toString().replace("-", ""), 1, name, sb.toString());
this.save(idMaker);
}
return idMaker;
});
if (idmaker.getCount() == 0){
idmaker.setCount(idmaker.getCount() + 1);
}else{
idmaker.upNo();
}
this.modify(idmaker);
return idmaker.getNo();
} finally {
lock.unlock();
}
}
}
架构思想实现篇:
@Component
public class SeqUtil {
@Autowired
private SeqRepo seqRepo;
private static SeqUtil seqUtil;
// 原子类
private static final AtomicLong autoNum = new AtomicLong(1);
// 日期
private static String curDateStr;
// 项目每次启动初始化
@PostConstruct
public void init() {
seqUtil = this;
seqUtil.seqRepo = this.seqRepo;
curDateStr = DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT_DAFAULT);
initSeq(curDateStr);
}
//初始化流程
private static void initSeq(String curDateStr) {
Seq seq = seqUtil.seqRepo.get(curDateStr);
if(Objects.isNull(seq)){
seqUtil.seqRepo.save(new Seq());
}else{
long nextAutoNo = seq.getAutoNo() + 100;
autoNum.set(nextAutoNo);
//更新
seq.setAutoNo(nextAutoNo);
seqUtil.seqRepo.modify(seq);
}
}
public static String genTimeCode(String preFix){
return genTimeCode(preFix, "yyyyMMdd");
}
private static String genTimeCode(String preFix, String dateFormat) {
String curDate = DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT_DAFAULT);
if(!curDateStr.equals(curDate)){
curDateStr = DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT_DAFAULT);
initSeq(curDateStr);
}
String seqNo = preFix + DateUtil.dateToStr(new Date(), dateFormat) + getAutoNum(autoNum.getAndIncrement());
if(autoNum.get() % 100 == 0){
Seq seq = seqUtil.seqRepo.get(DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT_DAFAULT));
seq.setAutoNo(autoNum.get());
seqUtil.seqRepo.modify(seq);
}
return seqNo;
}
private static String getAutoNum(long autoNo){
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 4 - String.valueOf(autoNo).length() ; i++) {
builder.append("0");
}
return builder.append(autoNo).toString();
}
}
model属性:
public class Seq extends Entity {
private String id;
private long autoNo; //自增编号
//创建时间、更新时间
private Date createTime;
private Date lastModifyTime;
public Seq(){
this.id = DateUtil.dateToStr(new Date(), DateUtil.DATE_FORMAT_DAFAULT);
this.autoNo = 1;
}
@Override
public String getId() {
return this.id;
}
public long getAutoNo() {
return autoNo;
}
public void setAutoNo(long autoNo) {
this.autoNo = autoNo;
}
public Date getCreateTime() {
return createTime;
}
public Date getLastModifyTime() {
return lastModifyTime;
}
}