热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么我使用SpringCache在Service中有缓存未命中-WhyIhavecachemissesinServiceusingSpringCache

Ihaveconfiguredmycacheasfollows:我已按如下方式配置缓存:@Configuration@EnableCachingpublicclassCach

I have configured my cache as follows:

我已按如下方式配置缓存:

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean(name = "caffeineCachingProvider")
    public CachingProvider caffeineCachingProvider() {
        return Caching.getCachingProvider("com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider");
    }

    @Bean(name = "caffeineCacheManager")
    public JCacheCacheManager getSpringCacheManager() {
        CacheManager cacheManager = caffeineCachingProvider().getCacheManager();
        CaffeineConfiguration> caffeineCOnfiguration= new CaffeineConfiguration<>();
        caffeineConfiguration.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.MINUTES, 60))));
        caffeineConfiguration.setCopierFactory(Copier::identity);
        cacheManager.createCache("informerCache", caffeineConfiguration);
        return new JCacheCacheManager(cacheManager);
    }
}

Also I have the @Service that uses it in following way:

我也有@Service以下列方式使用它:

@Service
public class InformerService {

    @CacheResult(cacheName = "informerCache")
    public List getProducts(@CacheKey String category, @CacheKey String countrySign, @CacheKey long townId) throws Exception {
        Thread.sleep(5000);
        // do some work
    }
}

So I have the next behavior.

所以我有下一个行为。

  1. When I'm calling the service method first time it takes 5 seconds then doing some work as expected.
  2. 当我第一次调用服务方法时需要5秒钟,然后按预期进行一些工作。
  3. Calling method the second time with same parameters - > caching works -> returns result immediately
  4. 第二次使用相同参数调用方法 - >缓存工作 - >立即返回结果
  5. Calling the third time with same parameters again results in Thread.sleep
  6. 再次使用相同参数调用第三次会导致Thread.sleep

And all over again.

并且一遍又一遍。

How to solve this ? Is that the issue about proxying ? What did I miss ?

怎么解决这个?这是关于代理的问题吗?我错过了什么 ?

1 个解决方案

#1


3  

As discussed in the comments, this was a bug in the JCache adapter. Thank you for letting me know about this problem. I released version 2.1.0 which includes this fix. That release also includes friendlier initial settings for CaffeineConfiguration which you identified in another post.

正如评论中所讨论的,这是JCache适配器中的一个错误。感谢您告诉我这个问题。我发布了包含此修复程序的2.1.0版。该版本还包括您在另一篇文章中确定的CaffeineConfiguration更友好的初始设置。

While the core library is heavily tested, the JCache adapters relied too heavily on the JSR's TCK (test compatibility kit). Unfortunately that test suite isn't very effective, so I added tests to help avoid these types of mistakes in the future.

虽然核心库经过严格测试,但JCache适配器过于依赖JSR的TCK(测试兼容性工具包)。不幸的是,测试套件不是很有效,因此我添加了测试以帮助避免将来出现这些类型的错误。

This issue only occurred in JCache because its version of expiration is not supported by Caffeine's core library. Caffeine prefers to use an O(1) design that eagerly cleans up expired entries by using fixed durations. JCache uses per-entry lazy expiration and the specification authors assume a capacity constraint is used to eventually discard expired entries. I added a warning to the documentation about this feature, as it could be error prone. While none of the other JCache implementations go beyond this, a pending task is to decide on a mechanism to help mitigate this JCache design flaw.

此问题仅发生在JCache中,因为Caffeine的核心库不支持其过期版本。咖啡因更喜欢使用O(1)设计,该设计通过使用固定的持续时间来急切地清理过期的条目。 JCache使用每个条目的延迟到期,并且规范作者假设容量约束用于最终丢弃过期的条目。我在文档中添加了有关此功能的警告,因为它可能容易出错。虽然其他JCache实现都没有超出此范围,但一项待定任务是决定一种机制来帮助缓解这种JCache设计缺陷。

Thanks again for reporting this issue. As always, feel free to reach out if you have any other issues or feedback to share.

再次感谢您报告此问题。与往常一样,如果您有任何其他问题或反馈要分享,请随时与我们联系。


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
author-avatar
寒月繁华叶落尽
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有