当前位置:网站首页>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 .
边栏推荐
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- Remote connection of raspberry pie by VNC viewer
- Eight honors and eight disgraces of the programmer version~
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- 聊聊内存模型与内存序
- [live broadcast appointment] database obcp certification comprehensive upgrade open class
- 门牌制作 C语言
- Catalogue of digital image processing experiments
- [adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
- Introduction to the latest plan of horizon in April 2022
猜你喜欢

Redis 过期策略+conf 记录

"A good programmer is worth five ordinary programmers!"

RuntimeError: no valid convolution algorithms available in CuDNN

How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!

【STL源码剖析】仿函数(待补充)

Li Kou brush questions (2022-6-28)

跨境电商如何通过打好数据底座,实现低成本稳步增长

高数有多难?AI 卷到数学圈,高数考试正确率 81%!

Redis expiration policy +conf record

2022 latest and complete interview questions for software testing
随机推荐
深度剖析数据在内存中的存储----C语言篇
Win11自动关机设置在哪?Win11设置自动关机的两种方法
What can I do after buying a domain name?
Brief introduction of emotional dialogue recognition and generation
Hisilicon VI access video process
[favorite poems] OK, song
C#中Linq用法汇集
跨境电商如何通过打好数据底座,实现低成本稳步增长
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
门牌制作 C语言
在SOUI里使用真窗口时使用SOUI的滚动条
Remote connection of raspberry pie by VNC viewer
"A good programmer is worth five ordinary programmers!"
Editor Caton
Golang common settings - modify background
Interface switching based on pyqt5 toolbar button -2
Doorplate making C language
简述中台的常识
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】