java—spring数据仓库中的默认方法会命中其他方法的缓存吗?

hgqdbh6s  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(334)

spring数据存储库中的默认方法是否会命中同一存储库中其他方法的缓存?
例如,调用此方法:

  1. default Long findIdByCodeRequired(final String code) {
  2. if (StringUtils.isBlank(code)) {
  3. throw new DataCheckException(MSG_CODE_REQUIRED, getEntityMessageArgument());
  4. }
  5. return findIdByCode(code).orElseThrow(notFound(code));
  6. }

点击这个方法的缓存?

  1. @Cacheable("codeType-findIdByCode")
  2. @Query("select c from #{#entityName} c " //
  3. + "where c.code = :code")
  4. Optional<Long> findIdByCode(String code);

或者默认方法不通过缓存工作所需的代理吗?

wsxa1bj1

wsxa1bj11#

所以答案是“不,存储库中的默认方法不会命中其他方法的缓存”

  1. @Test
  2. @Transactional
  3. public void testContactTypeCaching() {
  4. System.out.println("first call to findIdByCode");
  5. contactTypeRepo.findIdByCode("MOBILE");
  6. System.out.println("second call to findIdByCode");
  7. contactTypeRepo.findIdByCode("MOBILE");
  8. System.out.println("third call to findIdByCode");
  9. contactTypeRepo.findIdByCode("MOBILE");
  10. System.out.println("first call to getReferenceByCodeRequired");
  11. contactTypeRepo.getReferenceByCodeRequired("MOBILE");
  12. System.out.println("second call to getReferenceByCodeRequired");
  13. contactTypeRepo.getReferenceByCodeRequired("MOBILE");
  14. System.out.println("third call to getReferenceByCodeRequired");
  15. contactTypeRepo.getReferenceByCodeRequired("MOBILE");
  16. }

输出:

  1. first call to findIdByCode
  2. Hibernate:
  3. select
  4. contacttyp0_.ct_id as col_0_0_
  5. from
  6. contact_types contacttyp0_
  7. where
  8. contacttyp0_.code=?
  9. second call to findIdByCode
  10. third call to findIdByCode
  11. first call to getReferenceByCodeRequired
  12. Hibernate:
  13. select
  14. contacttyp0_.ct_id as col_0_0_
  15. from
  16. contact_types contacttyp0_
  17. where
  18. contacttyp0_.code=?
  19. second call to getReferenceByCodeRequired
  20. Hibernate:
  21. select
  22. contacttyp0_.ct_id as col_0_0_
  23. from
  24. contact_types contacttyp0_
  25. where
  26. contacttyp0_.code=?
  27. third call to getReferenceByCodeRequired
  28. Hibernate:
  29. select
  30. contacttyp0_.ct_id as col_0_0_
  31. from
  32. contact_types contacttyp0_
  33. where
  34. contacttyp0_.code=?
展开查看全部

相关问题