项目工程重启后,用RedisTemplate获取不了在redis中存在的数据

今天碰到一个很奇怪的问题,redis中插入的数据,在工程没有重启之前,是可以读取到的,工程重启之后,就读取不到了,但是登录redis的客户端查看,发现数据其实是存在的

用的下面这种方式读写:

    @Override
    public void addValue(String key, Object value, Long timeout) throws Exception {
        getValueOps().set(key, value, timeout, TimeUnit.DAYS);
    }
   
    @Override
    public Object getValue(String key) throws Exception {
        return getValueOps().get(key);
    }

 

经过调查,发现另外一种读写方式:

    @Override
    public void addValue(String key, Object value, Long timeout) throws Exception {
        redisTemplate.boundValueOps(key).set(value, timeout, TimeUnit.DAYS);
    }


    @Override
    public Object getValue(String key) throws Exception {
        return redisTemplate.boundValueOps(key).get();
    }

这样读取就不会出问题了,完整代码如下:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class KryoRedisSerializer<T> implements RedisSerializer<T> {

	Logger logger = LoggerFactory.getLogger(KryoRedisSerializer.class);
	
    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
    
    private Kryo kryo = new Kryo();
        
    @Override
    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return EMPTY_BYTE_ARRAY;
        }
        
        byte[] buffer = new byte[10240];
    	Output output = new Output(buffer);
        		
        try {
            kryo.writeClassAndObject(output, t);
            return output.toBytes();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally{
            closeOutputStream(output);
        }
 
        return EMPTY_BYTE_ARRAY;
    }
 
    @SuppressWarnings("unchecked")
	@Override
    public T deserialize(byte[] bytes) throws SerializationException {

    	if (bytes == null || bytes.length <= 0) {
            return null;
        }
    	
        Input input = new Input(bytes);
 
        try {
            return (T) kryo.readClassAndObject(input);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            closeInputStream(input);
        }
        return null;
    }
    
    private void closeOutputStream(Output output) {
        if (output != null) {
            try {
                output.flush();
                output.close();
            } catch (Exception e) {
                logger.error("serialize object close outputStream exception", e);
            }
        }
    }
    
    private void closeInputStream(Input input) {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e) {
                logger.error("serialize object close inputStream exception", e);
            }
        }
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

  @Autowired
  RedisTemplate<Object, Object> template;
	
  @Bean
  public RedisTemplate<Object, Object> redisStringTemplate() {
    template.setDefaultSerializer(new KryoRedisSerializer<>());
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new KryoRedisSerializer<>());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(new KryoRedisSerializer<>());
    return template;
  }

}
import java.util.List;
import java.util.Map;

public interface IRedisService {
  
  void addValue(String key,Object value, Long timeout) throws Exception;
  
  Object getValue(String key) throws Exception;
  
  void addList(String key,List<Object> list, Long timeout) throws Exception;

  List<Object> getList(String key) throws Exception;
  
  void addMap(String key,Map<String,Object> map, Long timeout) throws Exception;

  Map<String,Object> getMap(String key) throws Exception;
  
  void addValueSecond(String key, Object value, Long timeout) throws Exception;
  
  Long getExpire(String key);
	
  boolean hasKey(String key);
}
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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

import com.everestfortune.cf.service.IRedisService;

@Service
public class RedisServiceImpl implements IRedisService {
	
	@Autowired
	RedisTemplate<Object, Object> redisTemplate;

	@Override
	public void addValue(String key, Object value, Long timeout) throws Exception {
		redisTemplate.boundValueOps(key).set(value, timeout, TimeUnit.DAYS);
	}

	@Override
	public void addValueSecond(String key, Object value, Long timeout) throws Exception {
		redisTemplate.boundValueOps(key).set(value, timeout, TimeUnit.SECONDS);
	}
	
	@Override
	public Object getValue(String key) throws Exception {
		return redisTemplate.boundValueOps(key).get();
	}

	@Override
	public void addList(String key, List<Object> list, Long timeout) throws Exception {
		redisTemplate.boundValueOps(key).set(list, timeout, TimeUnit.DAYS);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getList(String key) throws Exception {
		return (List<Object>) redisTemplate.boundValueOps(key).get();
	}

	@Override
	public void addMap(String key, Map<String, Object> map, Long timeout) throws Exception {
		redisTemplate.boundValueOps(key).set(map, timeout, TimeUnit.DAYS);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getMap(String key) throws Exception {
		return (Map<String, Object>) redisTemplate.boundValueOps(key).get();
	}
	
	@Override
	public Long getExpire(String key){
		return redisTemplate.getExpire(key);
	}
	
	@Override
	public boolean hasKey(String key){
		return redisTemplate.hasKey(key);
	}
	
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学编程的司马光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值