接上一节。
先要说明的是上一节springboot缓存之使用redis作为缓存管理是springboot的旧版本了,最新的springboot2.x已经不这么用了,而且缓存注解一般用于service上,而不是controller上。百度了下整体代码是这样的:MyRedisConfig.java
EnableCaching // 开启缓存支持 public class MyRedisConfig extends CachingConfigurerSupport { @Resource private LettuceConnectionFactory lettuceConnectionFactory; /** * 配置CacheManager * @return */ @Bean public CacheManager cacheManager() { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 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); // 配置序列化(解决乱码的问题) RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() // .entryTtl(Duration.ZERO) .entryTtl(Duration.ofSeconds(20)) //设置缓存失效时间 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory) .cacheDefaults(config) .build(); return cacheManager; } /** * RedisTemplate配置 */ @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 设置序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); RedisSerializer<?> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer);// key序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化 redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } }
也可以通过这种方式来改变缓存:
package com.gong.springbootcache.service; import com.gong.springbootcache.bean.Department; import com.gong.springbootcache.mapper.DepartmentMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CacheConfig; import org.springframework.stereotype.Service; @CacheConfig(cacheNames = "dept") @Service public class DepartmentService { @Autowired DepartmentMapper departmentMapper; @Autowired CacheManager cacheManager; public Department getDeptById(Integer id){ Department department = departmentMapper.getDeptById(id); Cache cache = cacheManager.getCache("dept"); cache.put(String.valueOf(id),department); return department; } }
从缓存管理器中取得名为dept的缓存组件,然后设置缓存的key:id,以及缓存的值:department、
可参考:https://www.jianshu.com/p/d3ee0215c82e