当前位置:网站首页>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 .
边栏推荐
- Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- Prometheus deployment
- 门牌制作 C语言
- “一个优秀程序员可抵五个普通程序员!”
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
- 20220524_ Database process_ Statement retention
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- Doorplate making C language
猜你喜欢
Cryptography -- the mode of block cipher
基于Pyqt5工具栏按钮可实现界面切换-2
Interface switching based on pyqt5 toolbar button -1
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
Eight bit responder [51 single chip microcomputer]
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
采用VNC Viewer方式远程连接树莓派
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
实现BottomNavigationView和Navigation联动
随机推荐
[redis notes] compressed list (ziplist)
Connexion à distance de la tarte aux framboises en mode visionneur VNC
20220524_ Database process_ Statement retention
Go basic anonymous variable
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
Set right click to select vs code to open the file
Eight honors and eight disgraces of the programmer version~
万物并作,吾以观复|OceanBase 政企行业实践
Remote connection of raspberry pie by VNC viewer
深度剖析数据在内存中的存储----C语言篇
Call vs2015 with MATLAB to compile vs Project
海思调用接口之Makefile配置
Realize the linkage between bottomnavigationview and navigation
抖音实战~点赞数量弹框
Eight bit responder [51 single chip microcomputer]
公司里只有一个测试是什么体验?听听他们怎么说吧
门牌制作 C语言
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
[hardware] origin of standard resistance value
Solving ordinary differential equations with MATLAB