Springboot_cache 整合redis

github:https://github.com/july1992/springboot_cache

1:  添加依赖

 
            org.springframework.boot
            spring-boot-starter-data-redis
        

2: 重写redisconfig ,redis在存储的时候,会默认使用字节去存储,不方便查看,所以重写的目的是将结果转成json

Springboot_cache 整合redis_第1张图片Springboot_cache 整合redis_第2张图片

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 userRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {


        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer serializer=new Jackson2JsonRedisSerializer(User.class);
        template.setDefaultSerializer(serializer);
        return template;
    }


    /**
     *   参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration;
     *
     *     被动缓存调用的时候  结果转JSON
     */
    @Bean
    public RedisCacheManager userCacheManage(RedisConnectionFactory connectionFactory){


        Jackson2JsonRedisSerializer 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 :  其他的使用  如上篇一致 

你可能感兴趣的:(web)