一、问题描述
通过spring-data-redis依赖包的org.springframework.data.redis.core.ValueOperations的increment方法进行增长累加,然后再通过get方法获取累加结果,具体代码如下
String submitApiCountKey = "submitCountKey"; @b@ValueOperations<String, Long> operator = redisTemplate.opsForValue(); @b@operator.increment(submitApiCountKey, 1L); @b@long submitApiCount=operator.get(submitApiCountKey);
执行后,直接报“ERROR c.c.n.c.c.b.c.SerializationHanlder - com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 47”错误异常,详细日志如下
2022-02-09 15:54:23.799 {} [pool-3-thread-2] ERROR c.c.n.c.c.b.c.SerializationHanlder - com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 47@b@ at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:137) ~[kryo-4.0.0.jar:?] @b@ at com.esotericsoftware.kryo.Kryo.readClass(Kryo.java:693) ~[kryo-4.0.0.jar:?] @b@ at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:804) ~[kryo-4.0.0.jar:?]@b@ ...@b@ at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:335) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE] @b@ at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:61) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE]@b@ at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:225) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE] @b@ at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:185) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE]@b@ at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE] @b@ at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:53) ~[spring-data-redis-2.1.12.RELEASE.jar:2.1.12.RELEASE]
二、解决方法
将上面代码调整如下,即可解决问题
String submitApiCountKey = "submitCountKey"; @b@Boolean oprResult = (Boolean) redisTemplate.execute(new SessionCallback<Boolean>() { @b@ @Override @b@ public Boolean execute(RedisOperations redisOperations) throws DataAccessException { @b@ redisOperations.multi(); @b@ ValueOperations<String, Long> operator = redisOperations.opsForValue(); @b@ operator.increment(submitApiCountKey, 1L); @b@ operator.get(submitApiCountKey); @b@ List<Object> results = redisOperations.exec(); @b@ //条件逻辑判断@b@ return results.get(1)==1; @b@ } @b@@b@}); @b@@b@if(oprResult.booleanValue()){ @b@ //...条件逻辑后执行代码@b@}