springboot整合redis

第一步:导入依赖

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

第二步在springboot 的主程序类上添加如下

//扫描mybatis
@MapperScan(value = "com.aaa.cache.mapper")
@SpringBootApplication
//开启缓存
@EnableCaching
public class SpringBootCacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCacheApplication.class, args);
    }

}

第三步:

@CacheConfig(cacheNames = "emp",cacheManager = "redisCacheManager")
@Service
public class EmployyService {
    @Autowired
    EmployyMapper employyMapper;
    /**
     *
     * 根据方法的请求参数对其结果进行缓存
     *  CacheManager管理多个Cache组件,对缓存的真正CRUD操做在cache中,每一个缓存组件都有一个唯一的名称
     * value:指定缓存的名称
     *
     * */
    @Cacheable
    public Employee getEmp(Integer id){
        Employee employee = employyMapper.getEmployee(id);
        return  employee;
    }
}

第四步:自定义redis的缓存管理器(第一种方式)


@Configuration
public class RedisConfig {


    @Bean
    public RedisCacheManager redisCacheManager (RedisConnectionFactory redisConnectionFactory){
    //初始化一个redisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //初始化序列化方式为json;
        RedisSerializer redisSerializer = new GenericJackson2JsonRedisSerializer();
        //从序列器中获取fromSerializer(序列)
        RedisSerializationContext.SerializationPair pair =
                RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer);

        RedisSerializationContext.SerializationPair<String> stringSerializationPair =
                RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
        //设置默认的缓存配置为我们上述的序列化配置
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(pair)
                .serializeKeysWith(stringSerializationPair);

         // 设置过期时间
        // redisCacheConfiguration.entryTtl(Duration.ofSeconds(10));
        //禁止缓存null对象
        //redisCacheConfiguration.disableCachingNullValues();
        return new RedisCacheManager(redisCacheWriter,redisCacheConfiguration);
    }

第二种方式

@Configuration
public class RedisConfig {
/*  @Bean
    public RedisTemplate employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

       //Json序列化设置
        Jackson2JsonRedisSerializer redisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        template.setDefaultSerializer(redisSerializer);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(redisSerializer);

        template.setHashKeySerializer(stringRedisSerializer);

        template.afterPropertiesSet();
        return template;
    }
*/

/*
    @Bean
    @Primary
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                //.entryTtl(Duration.ofSeconds(10)) //设置过期时间
                .disableCachingNullValues() //禁止缓存null对象
                //定义了key和value的序列化协议,同时hash key和hash value也被定义
              .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(Employee.class)));

               // cacheConfiguration.computePrefixWith(cacheName -> "yourAppName".concat(":").concat(cacheName).concat(":"));


        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(cacheConfiguration)
                .build();
    }*/


}


 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(springboot)