Spring boot + redis + 序列化工具(四)

众所周知,使用redis,想要存入对象或者List对象,则需要给序列化一下

同时,我们在Spring boot中想要集成redis,首先,给引入jar

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-redisartifactId>
dependency>

并编写RedisCacheConfig.java文件,并在里面配置序列化方法

/**
 *
 * redis 缓存配置;
 * @version v.0.1
 *
 */
@Configuration
@EnableCaching // 启用缓存,这个注解很重要;
public class RedisCacheConfig extends CachingConfigurerSupport{

   /**
    * 缓存管理器.
    * @param redisTemplate
    * @return
    */
   @Bean
   public CacheManager cacheManager(RedisTemplate redisTemplate) {

          CacheManager cacheManager = new RedisCacheManager(redisTemplate);

          return cacheManager;

   }

/**
 *
 * redis模板操作类,类似于jdbcTemplate的一个类;
 *
 * 虽然CacheManager也能获取到Cache对象,但是操作起来没有那么灵活;
 *
 * 这里在扩展下:RedisTemplate这个类不见得很好操作,我们可以在进行扩展一个我们
 * 自己的缓存类,比如:RedisStorage类;
 * @param factory: 通过Spring进行注入,参数在application.properties进行配置;
 *
 * @return
 *
 */
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {

RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(factory);

MsgPackRedisSerializer redisSerializer = new MsgPackRedisSerializer(Object.class);

ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
redisSerializer.setObjectMapper(om);

redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
redisTemplate.setValueSerializer(redisSerializer);
redisTemplate.setHashValueSerializer(redisSerializer);


// Jackson2JsonRedisSerializer的方式来做这个序列化工具
//        StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
//        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);
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.afterPropertiesSet();

return redisTemplate;
}
}

其中MsgPackRedisSerializer.java 这个序列化工具是根据Jackson2JsonRedisSerializer类编写的,把其中的
其中的ObjectMapper new方法中的factory 换成了MessagePackFactory,使用messagePack这个序列化工具,实质上还是转成了json,中间经过了一个二进制过程。在redis可视化工具中可以看到数据。

public class MsgPackRedisSerializer implements RedisSerializer {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final JavaType javaType;
private ObjectMapper objectMapper;

public MsgPackRedisSerializer(Class type) {
objectMapper = new ObjectMapper();
javaType = getJavaType(type);
}

public MsgPackRedisSerializer(JavaType javaType) {
objectMapper = new ObjectMapper(new MessagePackFactory());
this.javaType = javaType;
}

public byte[] serialize(T t) throws SerializationException {

byte[] bs = null;

try {
bs = objectMapper.writeValueAsBytes(t);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

return bs;
}

public T deserialize(byte[] bytes) throws SerializationException {

if (bytes == null)
return null;
try {
return objectMapper.readValue(bytes, 0, bytes.length, javaType);
} catch (Exception ex) {
throw new SerializationException(
(new StringBuilder()).append("Could not read JSON: ").append(ex.getMessage()).toString(), ex);
}
}

public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}

protected JavaType getJavaType(Class clazz) {
return TypeFactory.defaultInstance().constructType(clazz);
}
}

你可能感兴趣的:(Redis,数据库)