当前位置:网站首页>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 .
边栏推荐
- 【Redis笔记】压缩列表(ziplist)
- Simple square wave generating circuit [51 single chip microcomputer and 8253a]
- Numerical solution of partial differential equations with MATLAB
- Go basic anonymous variable
- Use of recyclerview with viewbinding
- Where is the win11 microphone test? Win11 method of testing microphone
- Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
- Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- What experience is there only one test in the company? Listen to what they say
猜你喜欢
PotPlayer设置最小化的快捷键
C# MVC创建一个视图摆脱布局的影响
Go basic constant definition and use
CDN 加速,需要域名先备案
Golang common settings - modify background
Temperature measurement and display of 51 single chip microcomputer [simulation]
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
How does win11 turn on visual control? Win11 method of turning on visual control
深度剖析数据在内存中的存储----C语言篇
Li Kou brush questions (2022-6-28)
随机推荐
@BindsInstance在Dagger2中怎么使用
Why does RTOS system use MPU?
2016. maximum difference between incremental elements
非路由组件之头部组件和底部组件书写
LINQ usage collection in C #
golang中new与make的区别
[redis notes] compressed list (ziplist)
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
基于Pyqt5工具栏按钮可实现界面切换-1
程序员版本的八荣八耻~
RuntimeError: no valid convolution algorithms available in CuDNN
Quantitative analysis of PSNR, SSIM and RMSE
Redis expiration policy +conf record
基于Pyqt5工具栏按钮可实现界面切换-2
CDN 加速,需要域名先备案
C MVC creates a view to get rid of the influence of layout
(毒刺)利用Pystinger Socks4上线不出网主机
跨境电商如何通过打好数据底座,实现低成本稳步增长
Simple square wave generating circuit [51 single chip microcomputer and 8253a]