用redis为spring data jpa创建唯一id

@Service
public class IDServiceImpl implements IDService {

  @Resource
  private RedisTemplate<String, Serializable> redisTemplate;

  @Override
  public long generate(String key) {
    RedisAtomicLong counter = new RedisAtomicLong(key,
        Objects.requireNonNull(redisTemplate.getConnectionFactory()));
    return counter.incrementAndGet();
  }
}

使用redis的事务机制来创建并发情况下的唯一id

posted @ 2020-03-04 22:26  Geraltz'Rivia  阅读(191)  评论(0编辑  收藏  举报