玩转Spring Cache --- @Cacheable使用在MyBatis的Mapper接口上(解决Null key returned for cache operation)【享学Spring】

桃扇骨 2021-12-11 13:17 1416阅读 0赞

每篇一句

累吗?穷都不怕还怕累?只要干不死就往死里干

前言

据我观察,很多小伙伴学习一门技术一般都是度娘 + ctrl v的模式。比如本文的知识点,从网络的世界里你能找到有人介绍说:@Cacheable不仅仅能标注在实例方法上,也能标注在接口方法上

so,你回来试了试把它标注在自己的MyBatisMapper接口上,希望它能帮助分摊DB的压力。想法非常正派且看似可行,但一经实操却发现发现报错如下:

  1. java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder ...

顿时丈二的和尚了有木有,难道网上说法有误是个坑:@Cacheable不能使用在接口上吗?
其实都不是,而是因为Spring它只说了其一,并没有说其二。所以请相信,本文会给你意想不到的收获~

缓存注解使用在接口上的场景实用吗?

答:非常实用。

我们知道MyBatis作为一个优秀的、灵活的持久层框架,现在被大量的使用在我们项目中(国内使用Hibernate、JPA还是较少的)。并且我们大都是用Mapper接口 + xml文件/注解的方式去使用它来操作DB,而缓存作为缓解DB压力的一把好手,因此我们亟待需要在某些请求中在DB前面挡一层缓存。

举例最为经典的一个使用场景:DB里会存在某些配置表,它大半年都变不了一次,但读得又非常非常的频繁,这种场景还不在少数,因此这种case特别适合在Mapper接口层加入一层缓存,极大的减轻DB的压力~

本文目标

我们目标是:没有蛀牙–>能让缓存注解在Mapper接口上正常work~~~

Demo示例构造

现在我通过一个示例,模拟小伙伴们在MyBatis的Mapper接口中使用缓存注解的真实场景。

第一步:准备MyBatis环境

  1. @Configuration
  2. @MapperScan(basePackages = { "com.fsx.dao"})
  3. @PropertySource(value = "classpath:jdbc.properties", ignoreResourceNotFound = false, encoding = "UTF-8")
  4. public class MyBatisConfig {
  5. @Value("${datasource.username}")
  6. private String userName;
  7. @Value("${datasource.password}")
  8. private String password;
  9. @Value("${datasource.url}")
  10. private String url;
  11. // 配置数据源
  12. @Bean
  13. public DataSource dataSource() {
  14. MysqlDataSource dataSource = new MysqlDataSource();
  15. dataSource.setUser(userName);
  16. dataSource.setPassword(password);
  17. dataSource.setURL(url);
  18. return dataSource;
  19. }
  20. @Bean
  21. public SqlSessionFactoryBean sqlSessionFactory() {
  22. SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  23. factoryBean.setDataSource(dataSource());
  24. // factoryBean.setMapperLocations(); // 若使用全注解写SQL 此处不用书写Mapper.xml文件所在地
  25. return factoryBean;
  26. }
  27. }

准备Mapper接口(为了简便用MyBatis注解方式实现SQL查询):

  1. // @Repository //备注:这个注解是没有必要的 因为已经被@MapperScan扫进去了
  2. public interface CacheDemoMapper {
  3. @Select("select * from user where id = #{id}")
  4. @Cacheable(cacheNames = "demoCache", key = "#id")
  5. User getUserById(Integer id);
  6. }

单元测试:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes = { RootConfig.class, CacheConfig.class, MyBatisConfig.class})
  3. public class TestSpringBean {
  4. @Autowired
  5. private CacheDemoMapper cacheDemoMapper;
  6. @Autowired
  7. private CacheManager cacheManager;
  8. @Test
  9. public void test1() {
  10. cacheDemoMapper.getUserById(1);
  11. cacheDemoMapper.getUserById(1);
  12. System.out.println("----------验证缓存是否生效----------");
  13. Cache cache = cacheManager.getCache("demoCache");
  14. System.out.println(cache);
  15. System.out.println(cache.get(1, User.class));
  16. }
  17. }

看似一切操作自如,风平浪静,但运行后报错如下:

  1. java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public final com.fsx.bean.User com.sun.proxy.$Proxy51.getUserById(java.lang.Integer)] caches=[demoCache] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
  2. at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:578)
  3. ...

错误提示竟然告诉我没有key,不禁爆粗口:接口方法上注解里写的key = "#id"难道程序瞎吗?

报错原因分析

要相信:所有人都可能骗人,但程序不会骗人。
其实报错能给我们释放至少两个信号:

  1. 缓存注解确实开启而且生效了(若注解完全不生效,就不会报错)
  2. 缓存注解使用时,key为null而报错

从异常信息一眼就能看出,key为null了。但是我们确实写了key = "#id"为何还会为null呢?根据异常栈去到源码处:

因为前面有详细分析过缓存注解的处理过程、原理,因此此处只关心关键代码即可

  1. public abstract class CacheAspectSupport extends AbstractCacheInvoker implements BeanFactoryAware, InitializingBean, SmartInitializingSingleton {
  2. ...
  3. private Object generateKey(CacheOperationContext context, @Nullable Object result) {
  4. Object key = context.generateKey(result);
  5. if (key == null) {
  6. throw new IllegalArgumentException("Null key returned for cache operation (maybe you are " + "using named params on classes without debug info?) " + context.metadata.operation);
  7. }
  8. return key;
  9. }
  10. ...
  11. }

就是调用这个方法生成key的时候,context.generateKey(result);这一句返回了null才抛出了异常,罪魁祸首就是它了。继续看本类generateKey()这个方法:

  1. @Nullable
  2. protected Object generateKey(@Nullable Object result) {
  3. if (StringUtils.hasText(this.metadata.operation.getKey())) {
  4. EvaluationContext evaluationContext = createEvaluationContext(result);
  5. return evaluator.key(this.metadata.operation.getKey(), this.metadata.methodKey, evaluationContext);
  6. }
  7. return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
  8. }

从代码中可知,这是因为解析#id这个SpEL表达式的时候返回了null,到这就定位到问题的根本所在了。

若要继续分析下去,那就是和SpEL关系很大了。所以我觉得有必要先了解Spring的SpEL的解析过程和简单原理,若你还不了解,可以参照:【小家Spring】SpEL你感兴趣的实现原理浅析spring-expression~(SpelExpressionParser、EvaluationContext、rootObject)

其实导致SpEL返回null的最初原因,在于MethodBasedEvaluationContext这个类对方法参数的解析上。它解析方法参数时用到了ParameterNameDiscoverer去解析方法入参的名字,而关键在于:实现类DefaultParameterNameDiscoverer是拿不到接口参数名的。

DefaultParameterNameDiscoverer获取方法参数名示例

下面给出一个示例,方便更直观的看到DefaultParameterNameDiscoverer的效果:

  1. public static void main(String[] args) throws NoSuchMethodException {
  2. DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
  3. // 实现类CacheDemoServiceImpl能正常获取到方法入参的变量名
  4. Method method = CacheDemoServiceImpl.class.getMethod("getFromDB", Integer.class);
  5. String[] parameterNames = discoverer.getParameterNames(method);
  6. System.out.println(ArrayUtils.toString(parameterNames)); //{id}
  7. // 直接从method里拿 永远都是arg0/arg1...哦~~~需要注意
  8. Arrays.stream(method.getParameters()).forEach(p -> System.out.println(p.getName())); //arg0
  9. // 接口CacheDemoService不能获取到方法的入参
  10. method = CacheDemoService.class.getMethod("getFromDB", Integer.class);
  11. parameterNames = discoverer.getParameterNames(method);
  12. System.out.println(ArrayUtils.toString(parameterNames)); //{}
  13. method = CacheDemoMapper.class.getMethod("getUserById", Integer.class);
  14. parameterNames = discoverer.getParameterNames(method);
  15. System.out.println(ArrayUtils.toString(parameterNames)); //{}
  16. //虽然DefaultParameterNameDiscoverer拿不到,但是method自己是可以拿到的,只是参数名是arg0/arg1... 这样排序的
  17. Arrays.stream(method.getParameters()).forEach(p -> System.out.println(p.getName())); //arg0
  18. }

通过此例就不用我再多说了,知道上面的generateKey()方法返回null是肿么回事了吧~

怎么破?

问题已经定位了,我从来不缺解决方案。下面我给小伙伴们介绍三种,任君选择

方案一:使用a0/p0的方式去对方法入参进行引用

说了很多次了,key中使用SpEL表达式,即可用字段名,也可以用a0/p0这种按照顺序的方式去获取,形如这样:

  1. @Cacheable(cacheNames = "demoCache", key = "#a0")

运行一把试试,终于一切正常,并且缓存也生效了

  1. ----------验证缓存是否生效----------
  2. org.springframework.cache.concurrent.ConcurrentMapCache@709ed6f3
  3. User(id=1, name=fsx, age=21)

这种方案使用起来相对非常简单(把控好参数顺序),并且得到了源生支持无需额外开发,所以推荐使用~

方案二:自定义注解 + KeyGenerator

从之前的源码分析知道,如果自己不指定key这个属性,会交给KeyGenerator去自动生成,此方案就是以这个原理为理论基础实现的。

但是,难道需要给每个方法都定一个个性化的KeyGenerator来解决???
当然这样也是一种解决方案,但是也太麻烦了,现在此方案不可能落地的。所以本文需要结合一个自定义注解,绕开key这个属性然后加上一个**通用的KeyGenerator**来解决问题,下面我直接给出示例代码:

1、自定义一个自己的注解,绕过key,提供一个新属性mykey

  1. @Target({ ElementType.METHOD, ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. @Cacheable
  6. public @interface MyCacheable {
  7. @AliasFor("cacheNames")
  8. String[] value() default { };
  9. @AliasFor("value")
  10. String[] cacheNames() default { };
  11. String key() default "";
  12. String keyGenerator() default "";
  13. String cacheManager() default "";
  14. String cacheResolver() default "";
  15. String condition() default "";
  16. String unless() default "";
  17. boolean sync() default false;
  18. // 自定义的属性,代替key属性(请不要使用key属性了)
  19. String myKey() default "";
  20. }

2、准备一个通用的KeyGenerator来可以处理自定义注解的myKey属性:

  1. @EnableCaching // 使用了CacheManager,别忘了开启它 否则无效
  2. @Configuration
  3. public class CacheConfig extends CachingConfigurerSupport {
  4. @Bean(name = "myMethodParamKeyGenerator")
  5. public KeyGenerator myMethodParamKeyGenerator() {
  6. return (target, method, params) -> {
  7. //获得注解
  8. MyCacheable myCacheable = AnnotationUtils.findAnnotation(method, MyCacheable.class);
  9. if (myCacheable != null) {
  10. String myKey = myCacheable.myKey();
  11. if (myKey != null && StringUtils.hasText(myKey)) {
  12. //获取方法的参数集合
  13. Parameter[] parameters = method.getParameters();
  14. StandardEvaluationContext context = new StandardEvaluationContext();
  15. //遍历参数,以参数名和参数对应的值为组合,放入StandardEvaluationContext中
  16. // 注意:若没有java8的编译参数-parameters,参数名都回事arg0,arg1... 若有参数就是具体的参数名了
  17. for (int i = 0; i < parameters.length; i++) {
  18. context.setVariable(parameters[i].getName(), params[i]);
  19. }
  20. ExpressionParser parser = new SpelExpressionParser();
  21. //根据newKey来解析获得对应值
  22. Expression expression = parser.parseExpression(myKey);
  23. return expression.getValue(context, String.class);
  24. }
  25. }
  26. return params[0].toString();
  27. };
  28. }
  29. ...
  30. }

3、把新注解使用在我们的Mapper接口上:

  1. // @Repository //备注:这个注解是没有必要的 因为已经被@MapperScan扫进去了
  2. public interface CacheDemoMapper {
  3. @Select("select * from user where id = #{id}")
  4. @MyCacheable(cacheNames = "demoCache", /*key = "#a0"*/ keyGenerator = "myMethodParamKeyGenerator", myKey = "#arg0")
  5. User getUserById(Integer id);
  6. }

运行如上测试用例:缓存生效,正常work

此方案我个人也是比较推荐的,不仅仅能解决问题,而且还能达到炫技的效果。(不要说炫技无用,炫技从另外一方面能反映出你的实力,领导才能看中你嘛~~~当然,在生产环境下不要过度炫技)

方案三:开启Java8的-parameters 编译参数

方案二有个弊端,就是只能使用arg0、arg1这种方式引用到入参的值,使用起来不是特别的方便。原因是Java编译器在编译的时候就已经把Method的形参变量名抹去了。若想保留这个值,Java8提供了-parameters编译参数来实现:

此处处理编译以Idea为例,若是用maven编译的话,方式雷同。
步骤:setting-Java Compiler(如下图):
在这里插入图片描述

加入此参数后,编译后再运行。这时,你的myKey就只能这么写了:myKey = "#id"(#变量名x形式) 一切正常~

此种方其实我是并不推荐的,因为它还得强依赖于编译参数,有这种强依赖还是不太好

总结

虽然说程序员最重要的技能ctrl c加ctrl v。拿来主义固然是好,但是我建议还是得活出差异化,否则怎么脱颖而出呢?因此我是支持狂造你的代码,只有你的花招多了,你才是那个特别的你。

熟悉我写博文的小伙伴应该知道,我很少介绍一种技术的基本使用,而是注重乱造代码,因为我还是比较注重分享稍微高质量一些的知识,也希望前行的路上有你的支持和鼓励


关注A哥






































Author A哥(YourBatman)
个人站点 www.yourbatman.cn
E-mail yourbatman@qq.com
微 信 fsx641385712
活跃平台
公众号 BAT的乌托邦(ID:BAT-utopia)
知识星球 BAT的乌托邦
每日文章推荐 每日文章推荐

BAT的乌托邦

发表评论

表情:
评论列表 (有 0 条评论,1416人围观)

还没有评论,来说两句吧...

相关阅读