Spring Data存储库中的默认方法会影响其他方法的缓存吗? [英] Will default methods in Spring Data repositories hit caches on other methods?
本文介绍了Spring Data存储库中的默认方法会影响其他方法的缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Spring Data存储库中的默认方法是否会击中同一存储库中其他方法的缓存?
Will default methods in Spring Data repositories hit caches on other methods within the same repository?
例如,将调用此方法:
default Long findIdByCodeRequired(final String code) {
if (StringUtils.isBlank(code)) {
throw new DataCheckException(MSG_CODE_REQUIRED, getEntityMessageArgument());
}
return findIdByCode(code).orElseThrow(notFound(code));
}
在此方法上缓存吗?
@Cacheable("codeType-findIdByCode")
@Query("select c from #{#entityName} c " //
+ "where c.code = :code")
Optional<Long> findIdByCode(String code);
还是默认方法没有通过缓存才能正常工作所必需的代理?
Or do default methods not go through the proxy necessary for the caching to work?
推荐答案
所以答案是否,存储库中的默认方法不会在其他方法上访问缓存"
So the answer is "no, default methods in a repository will not hit the cache on other methods"
@Test
@Transactional
public void testContactTypeCaching() {
System.out.println("first call to findIdByCode");
contactTypeRepo.findIdByCode("MOBILE");
System.out.println("second call to findIdByCode");
contactTypeRepo.findIdByCode("MOBILE");
System.out.println("third call to findIdByCode");
contactTypeRepo.findIdByCode("MOBILE");
System.out.println("first call to getReferenceByCodeRequired");
contactTypeRepo.getReferenceByCodeRequired("MOBILE");
System.out.println("second call to getReferenceByCodeRequired");
contactTypeRepo.getReferenceByCodeRequired("MOBILE");
System.out.println("third call to getReferenceByCodeRequired");
contactTypeRepo.getReferenceByCodeRequired("MOBILE");
}
输出:
first call to findIdByCode
Hibernate:
select
contacttyp0_.ct_id as col_0_0_
from
contact_types contacttyp0_
where
contacttyp0_.code=?
second call to findIdByCode
third call to findIdByCode
first call to getReferenceByCodeRequired
Hibernate:
select
contacttyp0_.ct_id as col_0_0_
from
contact_types contacttyp0_
where
contacttyp0_.code=?
second call to getReferenceByCodeRequired
Hibernate:
select
contacttyp0_.ct_id as col_0_0_
from
contact_types contacttyp0_
where
contacttyp0_.code=?
third call to getReferenceByCodeRequired
Hibernate:
select
contacttyp0_.ct_id as col_0_0_
from
contact_types contacttyp0_
where
contacttyp0_.code=?
这篇关于Spring Data存储库中的默认方法会影响其他方法的缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文