本文介绍了spring-data-redis redisTemplate 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用 get() 方法时,发生了异常

When I call get() method, an exception occured

这是代码

@Service("RedisService")
public class RedisServiceImpl implements RedisService {

@Autowired
RedisTemplate<String, Long> redisTemplate;

@Override
public Long get(String key) {
    return redisTemplate.opsForValue().get(key);
}

@Override
public Long incrBy(String key, long increment) {
    return redisTemplate.opsForValue().increment(key, increment);
}

当我使用incrBy方法时,没有异常只有错误​​只有get方法




这是堆栈跟踪---

when I use incrBy method, there are no exceptions but only errors only get method





here is the stacktrace ---

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
    at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:198)
    at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
    at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:42)
    at net.daum.air21.bot.common.service.RedisServiceImpl.get(RedisServiceImpl.java:29)
    at net.daum.air21.bot.user.service.SeraCoffeeServiceImpl.getCurrentCount(SeraCoffeeServiceImpl.java:41)

推荐答案

默认情况下,RedisTemplate 使用 JdkSerializationRedisSerializer,所以如果你做了一个设置",它会让你的 Long 在 Redis 中看起来像这样:

By default, RedisTemplate uses a JdkSerializationRedisSerializer, so if you did a "set" it would make your Long look something like this in Redis:

"xacxedx00x05srx00x0ejava.lang.Long;x8bxe4x90xccx8f#xdfx02x00x01Jx00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x00x00x00x00x00x04"

IncrBy 有效,因为 Redis 总是从该操作返回 Long,因此 RedisTemplate 不会尝试反序列化结果.然而,get"的结果会经历反序列化过程,该过程需要与上述类似的格式.

IncrBy works because Redis always returns a Long from that operation, so RedisTemplate does not attempt to deserialize the result. The result of "get", however, goes through the deserialization process, which expects a format like the above.

您可以通过在 RedisTemplate 上使用不同的值序列化程序来解决此问题:

You can solve this by using a different value serializer on your RedisTemplate:

redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));

或者试试spring-data-redis自带的RedisAtomicLong类.

Or try the RedisAtomicLong class that comes with spring-data-redis.

这篇关于spring-data-redis redisTemplate 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 23:56