シーケンス符号化Utilをredisで生成


詳細

public class SequenceCodeUtils {

    /**
     *         
     * @param redisTemplate
     * @param prefix   
     * @param dailyIncNumSize      
     * @return
     */
    public static String genSeqCode(RedisTemplate redisTemplate, String prefix, int dailyIncNumSize){
        String thisDate = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String store_key = String.join(SequenceCodeUtils.class.getName(), prefix, thisDate);
        Long inc = redisTemplate.opsForValue().increment(store_key, 1);
        if (inc.compareTo(1L)==0) { //    ,      
            redisTemplate.expire(store_key, 2, TimeUnit.DAYS);
        }
        String increment =  String.valueOf(Math.round(Math.pow(10,dailyIncNumSize)) +inc).substring(1);
        return String.join("",prefix, thisDate, increment);
    }
}