/**
* 获取单个Id
*
* @return
*/
@GetMapping("/idGenerator")
public long getId() {
Long id = redisTemplate.opsForValue().increment("id");
return id;
}
/**
* 批量获取Id
*
* @param size
* @return
*/
@GetMapping("/idGenerator/{size}")
public List<Long> getIdBatch(@PathVariable(value = "size") int size) {
// 先获取到最大值,只请求一次redis
Long increment = redisTemplate.opsForValue().increment("id", size);
// 找到最小值
long startId = increment - size + 1;
List<Long> ids = new ArrayList<>(size);
for (long i = startId; i <= increment; i++) {
ids.add(i);
}
return ids;
}