首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-data-redis redisTemplate异常

spring-data-redis redisTemplate异常
EN

Stack Overflow用户
提问于 2013-06-18 15:14:11
回答 2查看 16.3K关注 0票数 5

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

以下是代码

代码语言:javascript
复制
@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方法

这是堆栈跟踪

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2013-06-24 02:26:02

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

代码语言:javascript
复制
"\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\x00\x04"

IncrBy之所以有效,是因为Redis总是从该操作返回一个长整型,所以RedisTemplate不会尝试反序列化结果。然而,"get“的结果会经过反序列化过程,该过程需要一个类似于上面的格式。

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

代码语言:javascript
复制
redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));

或者尝试一下spring-data-redis附带的RedisAtomicLong类。

票数 9
EN

Stack Overflow用户

发布于 2015-05-27 22:03:36

有点令人沮丧--谢谢你在RedisAtomicLong上的提示。但是这里有一个通过字符串键、字符串字段和长值使用HashOps的解决方案-使用Spring Boot来简化配置

代码语言:javascript
复制
@SpringBootApplication
@Configuration
public class DemoApplication {

    public static void main(String[] args) {
        final ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        final DacRepository dacRepository  = context.getBean(DacRepository.class);
        dacRepository.incrKeyExample();
    }


    @Bean
    public RedisTemplate<String, Long> getLongRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
         final RedisTemplate<String,Long> redisTemplate = new RedisTemplate<String, Long>();
         redisTemplate.setConnectionFactory(redisConnectionFactory);
         redisTemplate.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class));
         redisTemplate.setKeySerializer(new StringRedisSerializer());
         redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
         redisTemplate.setHashKeySerializer(new StringRedisSerializer());
         redisTemplate.afterPropertiesSet();
         return redisTemplate;
    }
}

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository

public class DacRepository {

    private final HashOperations<String, String, Long> hashOps;

    @Autowired
    private final RedisTemplate<String, Long> redisTemplate;

    @Autowired
    public DacRepository(RedisTemplate<String, Long> redisTemplate) {
        this.redisTemplate = redisTemplate;
        hashOps = redisTemplate.opsForHash();

    }


    public void incrKeyExample() {
        final Set<String> keys = this.redisTemplate.keys("*");
        for(String key: keys) {
            System.out.println("key: "+ key);
        }

        final String key = "deal-1";
        final String field = "view";
        final Long value = 1L;
        hashOps.put(key, field, value);
        final Long delta = 1L;
        hashOps.increment(key, field, delta);
        Long val = hashOps.get("deal-1", "view");
        System.out.println("Value = "+val);
    }

}




server.port=9001
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17162725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档