使用 RedisCacheManager
启动应用,在浏览器查询 /tesla/1166057546
查询多次,只会执行一次SQL,第一次执行时数据已被缓存在Redis中,后续通过读取Redis获得查询结果,重启项目,再次查询相同的数据
仍然不会执行SQL语句,数据已经缓存在redis中,不会像存储在CurrentMap中会随着项目重启清空缓存数据
查看Redis中保存的数据
这里存储的格式是按照默认的jdk序列化保存的,如果想要保存json格式的数据需要自定义CacheManager,指定序列化规则
自定义一个cacheManager
使用RedisCacheManagerBuilder的builder()方法创建RedisCacheManager
@Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader())); return builder.build(); } // RedisCacheManager配置类 private RedisCacheConfiguration determineConfiguration(ClassLoader classLoader) { RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<Tesla>(Tesla.class))); return config; } 复制代码
清空redis缓存,重启应用,再次查询 /tesla/1166057546
查看redis中存储的数据格式
已经变为JSON格式,但是这里的key相比使用ConcurrentMap存储结果时,key默认是目标方法传递的参数,这里多了一个前缀
这是因为默认的RedisCacheConfiguration配置类前缀
加前缀是否了防止key被覆盖,比如存储的Tesla的key为1,存储的Factory的key也为1,就会导致数据混乱。
可以调用这个方法来去除前缀或者其他方法来自定义前缀,参考org.springframework.data.redis.cache.RedisCacheConfiguration类中的其他方法
controller包下增加FactoryController
@RestController public class FactoryController { @Autowired private FactoryService factoryService; @GetMapping("/factory/{id}") public Factory find(@PathVariable("id") Integer id){ return factoryService.getFactoryById(id); } } 复制代码
service包下增加FactoryService接口和impl包下的FactoryServiceImpl
public interface FactoryService { Factory getFactoryById(Integer id); } 复制代码
@Service @Slf4j public class FactoryServiceImpl implements FactoryService { @Autowired private FactoryMapper factoryMapper; @Override @Cacheable(cacheNames = "factory") public Factory getFactoryById(Integer id) { log.info("查询" + id + "号超级工厂"); return factoryMapper.selectOneById(id); } } 复制代码
重新启动启动应用,在浏览器查询 /factory/1
控制打印出了一次SQL语句
清空控制台日志,再次查询
之前自定义的CacheManager用来处理Tesla,这个CacheManager管理所有的Cache,所以这里会出现将缓存中查到的Factory对象转换成Tesla对象,转换失败导致报错。
如何解决?在LilithConfig配置类中新增一个FactoryCacheManager,增加@Bean注解,专门处理Factory的序列化
@Bean public RedisCacheManager factoryCacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) { RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(this.redisCacheConfiguration( resourceLoader.getClassLoader()) .serializeValuesWith(RedisSerializationContext .SerializationPair .fromSerializer(new Jackson2JsonRedisSerializer<>(Factory.class)))); RedisCacheManager redisCacheManager = builder.build(); return redisCacheManager; } 复制代码
给service类上指定CacheManager
@CacheConfig(cacheManager = "cacheManager") public class TeslaServiceImpl implements TeslaService { //中间内容保持不变 } 复制代码
@CacheConfig(cacheManager = "factoryCacheManager") public class FactoryServiceImpl implements FactoryService { //中间内容保持不变 } 复制代码
重新启动该应用,再次查询 /factory/1
页面正常,数据也是从缓存中获取的,redis中保存的数据也是JSON格式的
查询Tesla也是正常的
上面都是通过注解来操作缓存,如果想要在代码中操作缓存,就需要通过cacheManager直接在代码中获取缓存,修改缓存
在FactoryController、FactoryService和FactoryServiceImpl中增加方法
@GetMapping("/factory/save/{id}") public Factory save(@PathVariable("id") Integer id){ return factoryService.opCache(id); } 复制代码
Factory opCache(Integer id); 复制代码
@Autowired private RedisCacheManager factoryCacheManager; public Factory opCache(Integer id){ log.info("手动将查询结果存入缓存,存入" + id + "号超级工厂"); Factory factory = factoryMapper.selectOneById(id); Cache cache = factoryCacheManager.getCache("2"); cache.put("2", cache); return factory; } 复制代码
重新启动,在浏览器中输入 /factory/save/2
查看redis中保存的数据
















