程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Data 存储库中的默认方法是否会命中其他方法的缓存?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Data 存储库中的默认方法是否会命中其他方法的缓存??

开发过程中遇到Spring Data 存储库中的默认方法是否会命中其他方法的缓存?的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Data 存储库中的默认方法是否会命中其他方法的缓存?的解决方法建议,希望对你解决Spring Data 存储库中的默认方法是否会命中其他方法的缓存?有所启发或帮助;

Spring Data 存储库中的默认方法是否会命中同一存储库中其他方法的缓存?

比如会调用这个方法

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);

还是默认方法不通过缓存工作所需的代理?

解决方法

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

    @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 存储库中的默认方法是否会命中其他方法的缓存?全部内容,希望文章能够帮你解决Spring Data 存储库中的默认方法是否会命中其他方法的缓存?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: