本章介绍SpringBoot与Redis整合,对缓存不太了解的可以参考【SpringBoot】SpringBoot 缓存(十八)
Redis安装参考:【Redis】安装及简单使用
SpringBoot与Redis整合参考:【SpringBoot】SpringBoot 整合Redis
SpringBoot使用Redis做缓存
1、使用debug=true的模式启动项目,查看日志,发现RedisCacheConfiguration匹配上了,而默认的
2、 查看RedisCacheConfiguration类,可以看到,在容器不存在CacheManager的时候,它就会自动注入了RedisCacheManager,通过前一章(【SpringBoot】SpringBoot 缓存(十八))SpringBoot缓存的学习,知道CacheManager是用来管理缓存的Cache的。其中CacheManager也是使用的JDK自动的序列化器
@Configuration @ConditionalOnClass(RedisConnectionFactory.class) @AutoConfigureAfter(RedisAutoConfiguration.class) @ConditionalOnBean(RedisConnectionFactory.class) // 条件在不存在CacheManager Bean的时候生效 @ConditionalOnMissingBean(CacheManager.class) @Conditional(CacheCondition.class) class RedisCacheConfiguration { private final CacheProperties cacheProperties; private final CacheManagerCustomizers customizerInvoker; private final org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration; RedisCacheConfiguration(CacheProperties cacheProperties, CacheManagerCustomizers customizerInvoker, ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration) { this.cacheProperties = cacheProperties; this.customizerInvoker = customizerInvoker; this.redisCacheConfiguration = redisCacheConfiguration.getIfAvailable(); } // 注入CacheManager @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(determineConfiguration(resourceLoader.getClassLoader())); List<String> cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { builder.initialCacheNames(new LinkedHashSet<>(cacheNames)); } return this.customizerInvoker.customize(builder.build()); } private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration( ClassLoader classLoader) { if (this.redisCacheConfiguration != null) { return this.redisCacheConfiguration; } Redis redisProperties = this.cacheProperties.getRedis(); org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration .defaultCacheConfig(); config = config.serializeValuesWith( // 使用默认的JDK序列化器 SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader))); if (redisProperties.getTimeToLive() != null) { config = config.entryTtl(redisProperties.getTimeToLive()); } if (redisProperties.getKeyPrefix() != null) { config = config.prefixKeysWith(redisProperties.getKeyPrefix()); } if (!redisProperties.isCacheNullValues()) { config = config.disableCachingNullValues(); } if (!redisProperties.isUseKeyPrefix()) { config = config.disableKeyPrefix(); } return config; } }
3、测试缓存,使用@Cacheable标签,如下,调用getEmp方法,查看缓存内容
1 @Cacheable(cacheNames="emp", key = "emp#id" /*keyGenerator = "myKeyGenerator"*/) 2 public Employee getEmp(Integer id){ 3 System.out.println("===查询" + id + "号员工"); 4 return employeeMapper.getEmpById(id); 5 }
缓存内容如下,存储的也是emp对象的序列化结果
4、自己注入一个CacheManager的Bean,并且使用json序列化的方式
1 /** 2 * 缓存管理器 3 */ 4 @Bean 5 public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { 6 7 //初始化一个RedisCacheWriter 8 RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); 9 10 //设置CacheManager的值序列化方式为json序列化 11 RedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); 12 RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair 13 .fromSerializer(jsonSerializer); 14 RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig() 15 .serializeValuesWith(pair); 16 17 //设置默认超过期时间是30秒 18 defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); 19 //初始化RedisCacheManager 20 return new RedisCacheManager(redisCacheWriter, defaultCacheConfig); 21 22 }
5、测试缓存,使用@Cacheable标签,如下,测试时,先将原有缓存情况,避免影响测试,调用getEmp方法,查看缓存内容