美好的一天,
目前,我对 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/