赞
踩
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory){
//重新设置Redis的数据模板,让其可以操作Java中的对象
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
public interface RedisService {
void set(String key, Object value);
Object get(String key);
Boolean delete(String key);
}
实现接口
@Service
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate<String,Object> redisTemplate;
public void set(String key, Object value) {
//更改在redis里面查看key编码问题
RedisSerializer redisSerializer =new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
vo.set(key, value);
}
public Object get(String key) {
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
return vo.get(key);
}
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。