1. /**
    2. * 获取单个Id
    3. *
    4. * @return
    5. */
    6. @GetMapping("/idGenerator")
    7. public long getId() {
    8. Long id = redisTemplate.opsForValue().increment("id");
    9. return id;
    10. }
    11. /**
    12. * 批量获取Id
    13. *
    14. * @param size
    15. * @return
    16. */
    17. @GetMapping("/idGenerator/{size}")
    18. public List<Long> getIdBatch(@PathVariable(value = "size") int size) {
    19. // 先获取到最大值,只请求一次redis
    20. Long increment = redisTemplate.opsForValue().increment("id", size);
    21. // 找到最小值
    22. long startId = increment - size + 1;
    23. List<Long> ids = new ArrayList<>(size);
    24. for (long i = startId; i <= increment; i++) {
    25. ids.add(i);
    26. }
    27. return ids;
    28. }