我有一个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;
}我可以在我的服务类中注入缓存对象。我可以使用这个缓存,并使用它进行get和put操作。
@Inject
@NamedCache(name="testCache")
private Cache<Object, Object> testCache;但是,当我用@CacheResult注释某个方法时,什么也没有发生。
@CacheResult(cacheName="testCache")
public Long simpleMethod(@CacheKey String key) {
return Long.valueOf(999);
}该方法的结果不在缓存中。我做错了什么?
发布于 2019-09-21 17:58:50
从你的代码中,我不能100%确定发生了什么。请注意,注释RI并没有直接使用String作为键,而是将所有内容包装到另一个对象中。请参阅:EE8 JCache annotation CacheResult doesn't work
如果这个提示没有帮助,请添加如何检查缓存内容或添加示例输出。
发布于 2019-09-23 18:30:03
您需要在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>https://stackoverflow.com/questions/58027507
复制相似问题