当前位置:网站首页>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 .
边栏推荐
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- Hisilicon VI access video process
- Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
- Call vs2015 with MATLAB to compile vs Project
- Eight honors and eight disgraces of the programmer version~
- Yolox enhanced feature extraction network panet analysis
- [hardware] origin of standard resistance value
- Interface switching based on pyqt5 toolbar button -2
- Solving ordinary differential equations with MATLAB
- Writing of head and bottom components of non routing components
猜你喜欢

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

购买完域名之后能干什么事儿?

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

BBR 遭遇 CUBIC

設置單擊右鍵可以選擇用VS Code打開文件

4 special cases! Schools in area a adopt the re examination score line in area B!

Hisilicon VI access video process

Print out mode of go

The use of 8255 interface chip and ADC0809
![[hardware] origin of standard resistance value](/img/d1/b9184c508d363eb0fa018ea6a57a10.png)
[hardware] origin of standard resistance value
随机推荐
Eight bit responder [51 single chip microcomputer]
C#中Linq用法汇集
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Makefile configuration of Hisilicon calling interface
[redis notes] compressed list (ziplist)
基于FPGA的VGA协议实现
20220527_ Database process_ Statement retention
C MVC creates a view to get rid of the influence of layout
2016. maximum difference between incremental elements
[analysis of STL source code] imitation function (to be supplemented)
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
設置單擊右鍵可以選擇用VS Code打開文件
万物并作,吾以观复|OceanBase 政企行业实践
[live broadcast appointment] database obcp certification comprehensive upgrade open class
[npuctf2020]ezlogin XPath injection
4 special cases! Schools in area a adopt the re examination score line in area B!
Brief introduction to common sense of Zhongtai
20220524_数据库过程_语句留档
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!