After the project is restarted, the data that exists in redis cannot be obtained with RedisTemplate

I encountered a very strange problem today. The data inserted in redis can be read before the project is restarted. After the project is restarted, it cannot be read. However, when you log in to the redis client to check, you find that the data is actually existing

Read and write in the following way:

    @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);
    }

 

After investigation, another way of reading and writing was found:

    @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();
    }

This way, there will be no problems with reading. The complete code is as follows:

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);
	}
	
}

 

Guess you like

Origin blog.csdn.net/u013282737/article/details/89402810