redisTemplate序列化以及集成方法
1、问题描述
大家运行redis得 keys *
命令时,查看key值,有的会发现以下情况, key值前面都有一些其它字符,但是不影响取key值,这是因为redisTemplate没有序列化:
2、redisTemplate序列化
使用redis时,添加以下代码即可序列化,该类可以作为一个工具类或配置类使用
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired(required = false)
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
//定义key序列化方式
//RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
//定义value的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// template.setKeySerializer(redisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
3、自定义封装的方法
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
public Object getObject(String key) {
ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
return valueOperations.get(key);
}
public <T> void hset(String key,String field,T value){
redisTemplate.opsForHash().put(key,field,value);
return;
}
public void hget(String key){
Map<String,Object> maps = redisTemplate.opsForHash().entries(key);
for (Map.Entry<String, Object> entry : maps.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= "
+ entry.getValue());
}
}
public Map<String, JSONObject> hscan(String key){
Map<String, JSONObject> map = new HashMap<>();
Cursor<Map.Entry<String, Object>> cursor = redisTemplate.opsForHash().scan(key, ScanOptions.NONE);
while (cursor.hasNext()){
Map.Entry<String, Object> entry = cursor.next();
//JSONObject jsonObject = new JSONObject();
//jsonObject.put(entry.getKey(), entry.getValue());
map.put(entry.getKey(), JSONObject.parseObject(entry.getValue().toString()));
}
return map;
}
public Map<Object,Object> hentries(String key){
Map<Object,Object> map = redisTemplate.opsForHash().entries(key);
return map;
}
public long hsize(String key){
return redisTemplate.opsForHash().size(key);
}
public boolean hasKey(String key,String field){
return redisTemplate.opsForHash().hasKey(key,field);
}
public <T> T hgetValue(String key,String field){
T value = (T) redisTemplate.opsForHash().get(key,field);
return value;
}
public <K,T> Map<K,T> hgetByKey(String key){
return redisTemplate.opsForHash().entries(key);
}
public void hdel(String key,String field){
redisTemplate.opsForHash().delete(key,field);
}
}