导读 这篇文章主要介绍了redis保存AtomicInteger对象踩坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
redis保存AtomicInteger对象踩坑

redisTemplate 保存AtomicInteger对象异常:

java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String
    at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)
    at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)
    at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139)

跟踪源码发现其执行的是 StringRedisSerializer 的实现,serialize默认接收的参数类型为String 从而抛出以上异常

经过检查,发现是RedisTemplate泛型惹的祸:

@Autowired
private RedisTemplate redisTemplate;
解决方案

去除泛型:

@Autowired
private RedisTemplate redisTemplate;

运行服务再次跟踪源码,执行的是 DefaultValueOperations 的实现,问题解决

RedisAtomicInteger的使用

RedisAtomicInteger 从名字上来说就是 redis 的原子Integer 数据类型,由于其原子性,可用于秒杀活动物品数量的控制。

以及保证顺序生成数字。

@Resource
  RedisTemplate redisTemplate;
 
 
  /**
   * RedisAtomicInteger
   *
   * @throws Exception 异常
   */
  @Test
  public void testTransaction1() throws Exception {
      RedisAtomicInteger redisCount = new RedisAtomicInteger("key1", this.redisTemplate.getConnectionFactory());
      redisCount.set(0);
      // 创建 100 个线程 并发执行  increment 操作
      ExecutorService pool = Executors.newFixedThreadPool(10);
      for (int i = 0; i < 100; i++) {
          pool.submit(() -> {
              // 配额码原子变量值增加,每次增加1
              for (int j = 0; j < 100; j++) {
                  int count = redisCount.incrementAndGet();
                  log.info(Thread.currentThread().getName() + ": " + count);
              }
          });
      }
  }

结果

.
.
.
pool-2-thread-90: 9989
pool-2-thread-61: 9987
pool-2-thread-3: 9986
pool-2-thread-12: 9990
pool-2-thread-25: 9991
pool-2-thread-90: 9992
pool-2-thread-12: 9994
pool-2-thread-61: 9993
pool-2-thread-25: 9995
pool-2-thread-61: 10000
pool-2-thread-12: 9996
pool-2-thread-61: 9997
pool-2-thread-25: 9998
pool-2-thread-12: 9999

原文来自:https://www.jb51.net/article/268201.htm

本文地址:https://www.linuxprobe.com/redis-atomicinteger-linux.html编辑:向云艳,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/