今天碰到一个很奇怪的问题,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);
}
}