java - infinispan中的javax.cache.annotation.CacheResult方法中是否需要参数

标签 java jakarta-ee infinispan jboss-eap-6 jsr107

美好的一天, 目前,我对 infinispan 的 JBoss Enterprise Application Platform 6.1 和 6.3 中的 javax.cache.annotation.CacheResult 实现有点困惑。

我一直在谷歌搜索并浏览 stackoverflow,但我还没有真正找到一个明确的答案,来解决这个一直让我忙碌的问题。那么我就开始吧。 @CacheResult 注解在其注解的方法中是否需要参数。它使用参数来实际创建商店的 key 。然而,它并没有真正记录如果没有的话会发生什么。对于想要返回存储在数据库中的国家/地区列表且该列表不会经常更改的 Web 应用程序来说,可能会发生这种情况。

代码示例:

/**
 * Fetches a list of Country's from the Database system trough the SOAP adapter.
 *
 * @return a lit of country's from the Database system.
 */
@CacheResult(cacheName = "referenceService/CountryListCache")
// The attributes sorted is used by the caching mechanism, as combined key for the (to be) cached CountryListCache object.
public List<Country> getCountryList() throws ReferenceServiceException_Exception {
    LOG.debug("Cache is not used doing a full call to the service.");
    ReferenceTableIdSO getter = //Create a getter that does the external query;

    List<AlfaCodeDBObjectSO> transform = //calls the external system to get the data.
    //Transform the external data to somtine we want to return.
    List<Country> result = new ArrayList<Country>();
    for (AlfaCodeDBObjectSO trans : transform) {
        Country country = new Country(trans.getCode(), trans.getExplanation());
        result.add(country);
    }

    return result;
}

EAP6.1.1配置

    <subsystem xmlns="urn:jboss:domain:infinispan:1.4">
        <cache-container name="rest-cache" default-cache="default" start="EAGER"> 
            <local-cache name="default">  
                <transaction mode="NONE"/>
                <eviction strategy="LRU" max-entries="1000"/>
                <expiration max-idle="3600000"/>
            </local-cache>
            <local-cache name="referenceService/CountryListCache">  
                <locking isolation="REPEATABLE_READ" acquire-timeout="15000"/>
                <transaction mode="NONE" locking="PESSIMISTIC"/>
                <eviction strategy="LRU" max-entries="1024"/>
                <expiration lifespan="86400000"/>
            </local-cache>
        </cache-container>
    </subsystem>

正如您在我的示例中看到的,我希望减少代码调用。为了确定起见,我将配置设置为每天刷新一次。但我实际上不确定列表是否被缓存等。因为关于该方法是否没有参数的记录非常少。

最佳答案

根据the specification for JSR-107 ,如果未指定cacheName,则@CacheResult

  • 在类的 @CacheDefaults 注释中查找 cacheName 属性。正如您可以从名称中推断出的,此注释为类中与缓存相关的注释设置默认值。如果没有设置该注解,则

  • 使用模板package.name.ClassName.methodName(package.ParameterType,package.ParameterType)自动生成cacheName

编辑:我之前误读了你的问题。关于无参数方法的 key 生成,虽然规范没有直接解决,但从以下角度可以推断不支持

  • Infinispan 实现 DefaultCacheKey (缓存中键的包装类)显示无参数方法将导致 deepHashCode对于该方法的每次调用,为 0。因此,您的缓存将只包含一个值,且键为 0

  • JSR 中的 getAnnotations() 方法具有以下注释:

    @return An immutable Set of all Annotations on this method parameter,never null.

    这表明(无论如何对我来说)缓存的方法不会没有参数。

<小时/>

您可能想重新评估您的设计:我想不出应该缓存无参数方法调用的原因。我可以想到无参数方法的两个用例

  • 该方法的每次调用都会返回一个唯一的值,在这种情况下,您不想将缓存添加到组件的该级别,而是添加到父方法因为缓存第一次调用后不会使用的唯一值没有任何值(value)

  • 该方法的每次调用都会返回相同的值,在这种情况下,调用该方法一次(可能在启动时)并存储返回值以在应用程序的持续时间内使用可能会更干净

关于java - infinispan中的javax.cache.annotation.CacheResult方法中是否需要参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843930/

相关文章:

java - 使用特定的java参数启动tomcat

java - 如何将图像数组传递给 JSONObject?

java - 为什么 Swarm 注册 "jpa"、 "undertow"缓存容器

java - 对可能包含数字的字符串进行排序

java - 您会推荐在 Java 中使用哪个 SVG 工具包?

java - 使用 iTextPdf FontFactory 从/WEB-INF/resources/fonts/foobar.ttf 加载字体

java - Infinispan JMX 错误

infinispan - 如何处理 Infinispan 持久缓存存储中 SoftIndexFileStore 的旧文件?

java - 给定 View 类 [org.springframework.web.servlet.view.tiles3.TilesView] 不是 [org.springframework.web.servlet.view.AbstractUrlBasedView] 类型

java - org.bouncycaSTLe.tls.crypto.TlsCertificate getSubject() 和其他 getter