Redis生成自增流水号,每日清零
生活随笔
收集整理的這篇文章主要介紹了
Redis生成自增流水号,每日清零
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** 序列號Service業(yè)務層處理,最終樣式舉例 2021/11/26-23** @author 蟬沐風* @date 2021-05-28*/
@Service
public class ISerialServiceImpl implements ISerialService {@Autowiredprivate RedisCache redisCache;@Autowiredpublic RedisTemplate redisTemplate;public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd-");@Overridepublic String generateSerial() {//日期前綴String prefix = sdf.format(new Date());//如果日期前綴未過期,則序號自增//否則,將日期作為Key,1作為Value重置,并設置第二天0點過期if (redisCache.hasKey(prefix)) {redisTemplate.opsForValue().increment(prefix, 1);} else {redisTemplate.opsForValue().set(prefix, 1);redisTemplate.expireAt(prefix, getMidnightDate());}return prefix + redisTemplate.opsForValue().get(prefix);}// 獲取第二天00:00的時間private static Date getMidnightDate() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.add(Calendar.DATE, 1);return calendar.getTime();}}
我是蟬沐風,一個讓你沉迷于技術的講述者,歡迎大家留言!
總結
以上是生活随笔為你收集整理的Redis生成自增流水号,每日清零的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 所谓数据结构,对python来说不过如此
- 下一篇: Oracle9i数据库Data Guar