RedisTemplate 使用 increasement() 和 get() 时报 SerializationException

https://cloud.tencent.com/developer/article/1706934

 

org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedExcept
ion: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.EOFException
        at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84)
        at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:360)
        at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:62)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:223)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:190)
        at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:97)
        at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:54)

 

 

 

简单来说调用incr后得到 值不会出错是没有经过redistemplate的deserialize, 而get必须经过

所以,第一次调用 get 时,因为没有值,所以是成功的。然后调用了 incr 成功。第二次再调用 get 时,反序列化失败了

 

使用 RedisAtomicInteger 来处理:org.springframework.data.redis.support.atomic.RedisAtomicInteger

https://blog.csdn.net/u013107634/article/details/89500541

 

 

------

看了下 RedisAtomicInteger 的实现,都是将 key,value 转成了 String 之后存放到 redis 中的,看来 redis 还是最适合存放 string 类型的数据。

posted on 2023-12-14 20:29  快鸟  阅读(119)  评论(0编辑  收藏  举报