redis每天生成自增流水號(001、002...)
阿新 • • 發佈:2020-09-17
原理:
利用redis的RedisAtomicLong類實現該功能:
讓其每天第一次放置一個新的自增的值(一天過期)
然後和每天的日期相加就可以了
例子: 20180901 + 001 ;當天就是 20180901 + 002
如果要多少個0,可以自己配置(工具類中)
一、pom.xml配置
<!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency>
二、redisTemplate配置
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration publicclass RedisConfig { /** * retemplate相關配置 * @param factory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 配置連線工廠 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(預設使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修飾符範圍,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化輸入的型別,類必須是非final修飾的,final修飾的類,比如String,Integer等會跑出異常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSeial.setObjectMapper(om); // 值採用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer來序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 設定hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); return template; } }
三、程式碼實現
import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.support.atomic.RedisAtomicLong; import org.springframework.stereotype.Component; @Component public class CacheService { //這裡因為有其他的template,所以名字起得不好看 @Autowired RedisTemplate<String, Object> ObjectRedisTemplate; public Long getIncrementNum(String key) { // 不存在準備建立 鍵值對 RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory()); Long counter = entityIdCounter.incrementAndGet(); if ((null == counter || counter.longValue() == 1)) {// 初始設定過期時間 System.out.println("設定過期時間為1天!"); entityIdCounter.expire(1, TimeUnit.DAYS);// 單位天 } return counter; } }
四、工具類(可以直接新增方法)
public class SequenceUtils { static final int DEFAULT_LENGTH = 3; public static String getSequence(long seq) { String str = String.valueOf(seq); int len = str.length(); if (len >= DEFAULT_LENGTH) {// 取決於業務規模,應該不會到達3 return str; } int rest = DEFAULT_LENGTH - len; StringBuilder sb = new StringBuilder(); for (int i = 0; i < rest; i++) { sb.append('0'); } sb.append(str); return sb.toString(); } }
五、測試
String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date()); Long num = cacheService.getIncrementNum("demo_get_the_new_" + "test3_"+currentDate); String flowCode = SequenceUtils.getSequence(num); logger.info("流水號: " + flowCode);
六、控制檯輸出
設定過期時間為1天! 流水號0001 設定過期時間為1天! 流水號0002