当前位置:网站首页>Use redis to realize self increment serial number
Use redis to realize self increment serial number
2022-07-02 23:25:00 【Distant nenuda】
Use Redis Realize self increment serial number
Scenario requirements
There is a scenario in the project that a serial number needs to be generated after the guest meal application is approved , The rule is :202202150001,202202150002,qian' How many are month, year and day , The last four digits increase in turn .
The thought of Redis It's memory based , And it's fast , It also does not occupy database resources . Therefore, it is adopted Redis How to achieve .
Code implementation
Form rule tool class :
/**
* @author Greenarrow
* @date 2022-02-15 8:01
**/
public class SequenceUtil {
static final int DEFAULT_LENGTH = 4;
/**
* format key
* @param seq
* @return
*/
public static String getSequence(Long seq) {
String str = String.valueOf(seq);
int len = str.length();
// Depending on the size of the business
if (len >= DEFAULT_LENGTH) {
return str;
}
int rest = DEFAULT_LENGTH - len;
StringJoiner stringJoiner = new StringJoiner("");
for (int i = 0; i < rest; i++) {
stringJoiner.add("0");
}
stringJoiner.add(str);
return stringJoiner.toString();
}
/**
* Get the current date
* @return
*/
public static String getCurrentDate(){
LocalDate localDate = LocalDate.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
return df.format(localDate);
}
}
Get increment :
/**
* Get increment
* @param key
* @return
*/
public long incr(String key){
return stringRedisTemplate.opsForValue().increment(key);
}
Concrete realization :
String currentDate = SequenceUtil.getCurrentDate();
String key = "serial.number:" + currentDate;
long sequence = redisService.incr(key);
String seq = SequenceUtil.getSequence(sequence);
StringJoiner sj = new StringJoiner("");
sj.add(currentDate).add(seq);
System.out.println(sj.toString());
Output results :
202202150001
202202150002
202202150003
202202150004
Local Redis Data in the library : 
Expanding knowledge
Redis There are two strategies for serialization , Namely StringRedisTemplate and RedisTemplate, among StringRedisTemplate String for operation ,RedisTemplate It uses JDK Default binary serialization .
Everybody knows redis Serialization is to change key,value The value is first converted to the form of a stream , Store it in redis in .
RedisTemplate It is used. JdkSerializationRedisSerializer serialize , The serialized value contains object information , Version number , Class information, etc , It's a string , Therefore, it is impossible to increase the value by itself .
and StringRedisTemplate The serialization strategy is to convert the value of a string directly into a byte array , So store it in redis In is the value , Therefore, self increment operation can be carried out .
边栏推荐
- Submit code process
- Value sequence < detailed explanation of daily question >
- 跨境电商如何通过打好数据底座,实现低成本稳步增长
- Loss function~
- ServletContext learning diary 1
- Boost库链接错误解决方案
- 采用VNC Viewer方式远程连接树莓派
- Cryptographic technology -- key and ssl/tls
- Writing of head and bottom components of non routing components
- Application of containerization technology in embedded field
猜你喜欢

Ideal car × Oceanbase: when the new forces of car building meet the new forces of database

RuntimeError: no valid convolution algorithms available in CuDNN

Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup

理想汽车×OceanBase:当造车新势力遇上数据库新势力

What can I do after buying a domain name?

Why does RTOS system use MPU?

“一个优秀程序员可抵五个普通程序员!”

C#中Linq用法汇集

抖音实战~点赞数量弹框
![Eight bit responder [51 single chip microcomputer]](/img/4f/683948fca93a0dc1a0409839f60759.jpg)
Eight bit responder [51 single chip microcomputer]
随机推荐
[Yangcheng cup 2020] easyphp
Why does RTOS system use MPU?
Which common ports should the server open
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
PHP get real IP
BBR encounters cubic
2022年最新最全软件测试面试题大全
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Ping domain name error unknown host, NSLOOKUP / system d-resolve can be resolved normally, how to Ping the public network address?
Markdown basic grammar
密码技术---密钥和SSL/TLS
Interface switching based on pyqt5 toolbar button -2
Three solutions to frequent sticking and no response of explorer in win11 system
采用VNC Viewer方式远程连接树莓派
Brief introduction of emotional dialogue recognition and generation
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
golang中new与make的区别
Doorplate making C language
Solving ordinary differential equations with MATLAB
Where is the win11 microphone test? Win11 method of testing microphone