java 任务id每天重置并自增

生成规则

当前年份(省略年份前三位数)+月份+日期+三位顺序码,比如2021年7月15日第3笔。 此编号对应为:10715003

实现思路

  1、使用redis原子自增特性

  2、先判断key,是否存在

    2.1、存在:顺序码自增

    2.2、不存子:重新生成顺序码

代码实现

控制器

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author:chenyanbin
 */
@RestController
@RequestMapping("/api/task/v1")
@Api(tags = "任务API 测试")
public class TaskController {
    @Autowired
    RedisService redisService;
    /**
     * 自增序号有效期,一天半
     */
    public static final Long EXPIRE = 60 * 60 * (24 + 12) * 1L;

    /**
     * 生成任务规则如下
     * 当前年份(省略年份前三位数)+月份+日期+三位顺序码,比如2021年7月15日第3笔。 此编号对应为:10715003
     *
     * @return
     */
    @ApiOperation("生成任务编号")
    @GetMapping("generator")
    public String generateTaskId() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yMMdd");
        String key = dtf.format(localDateTime);
        long incr;
        if (redisService.exists(key)) {
            incr = redisService.incr(key, EXPIRE);
        } else {
            incr = redisService.incr(key, EXPIRE);
        }
        return key.substring(key.length() - 5) + CommonUtil.automaticFilling((int) incr, 3);
    }
}

redis工具类

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 * @Author:chenyanbin
 */
@Service
@Slf4j
public class RedisService {
    @Autowired
    private RedisTemplate redisTemplate;
    private static double size = Math.pow(2, 32);

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 写入缓存
     *
     * @param key        缓存key
     * @param value      缓存value
     * @param expireTime 过期时间,秒
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        return true;
    }

    /**
     * 原子递增
     *
     * @param key        键
     * @param expireTime 过期时间,秒
     * @return
     */
    public Long incr(final String key, Long expireTime) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        Long increment = operations.increment(key);
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        return increment;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

}

自动补零工具类

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 公共工具类
 *
 * @Author:chenyanbin
 */
@Slf4j
public class CommonUtil {

    /**
     * 自动补位
     * @param code 数值
     * @param num 保留的位数
     * @return
     */
    public static String automaticFilling(int code, int num) {
        return String.format("%0" + num + "d", code);
    }
}

演示

注意事项

  使用Redis原子自增特性,设置缓存失效时间1天半隔天顺序号需要重置,所以缓存没必要存那么久~~~~~~

posted @ 2021-09-24 23:23  陈彦斌  阅读(1663)  评论(0编辑  收藏  举报