github:https://github.com/july1992/springboot_cache
1: Add dependence
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2: Rewrite redisconfig, redis when stored defaults to byte to store, convenient viewing, so the purpose is to rewrite the results converted into json
3: MyRedisConfig
package com.vily.springboot_jpa_cache2.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vily.springboot_jpa_cache2.bean.User;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.net.UnknownHostException;
/**
* * description : 创建自己的序列化器,
* 这样子在存redis的时候 就会自动转译成json
* * Author : Vily
* * Date : 2019-06-04
*
**/
@Configuration
public class MyRedisConfig {
public MyRedisConfig() {
}
/**
* 参考RedisAutoConfiguration, 主动缓存调用的时候 转JSON
*/
@Bean
public RedisTemplate<Object, User> userRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, User> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<User> serializer=new Jackson2JsonRedisSerializer<User>(User.class);
template.setDefaultSerializer(serializer);
return template;
}
/**
* 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration;
*
* 被动缓存调用的时候 结果转JSON
*/
@Bean
public RedisCacheManager userCacheManage(RedisConnectionFactory connectionFactory){
Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
redisSerializer.setObjectMapper(objectMapper);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer));
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build();
return redisCacheManager;
}
}
4: other uses consistent with the above articles