我有JavaEE应用程序,正在尝试将cache2k与jCache一起使用以缓存某些功能的结果。当我调用以@CacheResult注释的方法时,什么也没有发生。我被困住了,不知道怎么了。
我将这些依赖项添加到了pom文件中:
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-jcache</artifactId>
<version>1.2.4.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-api</artifactId>
<version>1.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.jsr107.ri</groupId>
<artifactId>cache-annotations-ri-cdi</artifactId>
<version>1.1.1</version>
</dependency>
我将cache2k.xml添加到了我的类路径中。
<cache2k xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='https://cache2k.org/schema/v1.x'
xsi:schemaLocation="https://cache2k.org/schema/v1.x https://cache2k.org/schema/cache2k-core-v1.x.xsd">
<!-- The configuration example for the documentation -->
<version>1.0</version>
<skipCheckOnStartup>false</skipCheckOnStartup>
<defaults>
<cache>
<entryCapacity>1000</entryCapacity>
<sections>
<jcache>
<copyAlwaysIfRequested>true</copyAlwaysIfRequested>
<supportOnlineListenerAttachment>true</supportOnlineListenerAttachment>
</jcache>
</sections>
<sharpExpiry>true</sharpExpiry>
</cache>
</defaults>
<templates>
<cache>
<name>neverExpire</name>
<eternal>true</eternal>
</cache>
<cache>
<name>regularExpiry</name>
<expireAfterWrite>5m</expireAfterWrite>
</cache>
<cache>
<name>lessResilient</name>
<resilienceDuration>1m</resilienceDuration>
</cache>
</templates>
<caches>
<cache>
<name>testCache</name>
<include>neverExpire</include>
</cache>
</caches>
</cache2k>
我有用于缓存注入的生产者方法。我在那里注册了所有缓存事件(onCreated,onUpdated,onRemoved,onExpired)的侦听器,以用于调试。
@Produces
@NamedCache
public Cache<Object, Object> getCache(InjectionPoint injectionPoint) {
final CachingProvider cachingProvider = Caching.getCachingProvider();
final CacheManager mgr = cachingProvider.getCacheManager();
final NamedCache annotation = injectionPoint.getAnnotated().getAnnotation(NamedCache.class);
final String cacheName = annotation.name();
Cache<Object, Object> cache = mgr.getCache(cacheName);
if (cache == null) {
System.out.println("CACHE CREATE");
MutableConfiguration<Object, Object> config = new MutableConfiguration<Object, Object>();
config.setTypes(Object.class, Object.class);
config.setStoreByValue(true);
config.setStatisticsEnabled(false);
config.setManagementEnabled(false);
config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));
cache = mgr.createCache(cacheName, config);
}
else {
System.out.println("CACHE FOUND");
}
cache.registerCacheEntryListener(
new MutableCacheEntryListenerConfiguration<Object, Object>(
FactoryBuilder.factoryOf(new CacheListener<Object, Object>()),
null,
false,
false));
return cache;
}
我可以在服务类中注入缓存对象。我可以使用此缓存并对其进行获取和放置操作。
@Inject
@NamedCache(name="testCache")
private Cache<Object, Object> testCache;
但是当我用@CacheResult注释某些方法时,什么也没发生。
@CacheResult(cacheName="testCache")
public Long simpleMethod(@CacheKey String key) {
return Long.valueOf(999);
}
该方法的结果不在高速缓存中。 我在做什么错了?
答案 0 :(得分:0)
从您的代码中,我不确定100%发生了什么。请注意,注释RI不是直接使用String
作为键,而是将所有内容包装到另一个对象中。参见:EE8 JCache annotation CacheResult doesn't work
如果此提示没有帮助,请添加检查缓存内容的方式或添加示例输出。
答案 1 :(得分:0)
您需要在beans.xml中注册CacheResultInterceptor。像这样:
/META-INF/beans.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>org.jsr107.ri.annotations.cdi.CacheResultInterceptor</class>
</interceptors>
</beans>