直接上代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82package com.fuli.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Calendar; /** * @description: 通过Redis生成序列号 * @author: YqZhilan * @date: 2020-08-01 */ @Component public class RedisSequenceUtil { @Autowired private RedisUtil redisUtil; private final long delta = 1; // 默认步长 /*** * 生成序列值的前半段 * @param strs * @return */ public String generateSequenceF(String... strs) { StringBuilder sb = new StringBuilder(); if (strs != null) { for (int i = 0; i < strs.length; i++) { sb.append(strs[i]); } } return sb.toString(); } /*** * 获取下一个序列号 * @param key Redis Key值 * @param value 初始值 * @param timeout 过期时间 * @return */ public String setSequence(String key, long value, long timeout) { redisUtil.set(key, value, timeout); return value+""; } /*** * 获取当前序列号 * @param key Redis Key值 * @return */ public String getCurrentSequence(String key) { Object obj = redisUtil.get(key); return obj == null ? "" : obj.toString(); } /*** * 获取下一个序列号 * @param key Redis Key值 * @return */ public String getNextSequence(String key) { return this.getNextSequence(key, this.delta); } /*** * 获取下一个序列号 * @param key Redis Key值 * @param delta 增长步长 * @return */ public String getNextSequence(String key, long delta) { redisUtil.incr(key, delta); Object obj = redisUtil.get(key); return obj == null ? "" : obj.toString(); } /*** * 获取本天剩余的秒数 * @return */ public long getLeftSeconsOfDay() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000; } }
说明:
复制代码
1private RedisUtil redisUtil; 该类仅是一个Redis操作的工具类,可以自己封装。
至此结束 ... ...
最后
以上就是悦耳荷花最近收集整理的关于技术工具类:通过Redis的自增序列来生成系统使用的序列号的全部内容,更多相关技术工具类:通过Redis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复