首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在cache2k中使用@CacheResult注释的正确方式是什么

在cache2k中使用@CacheResult注释的正确方式是什么
EN

Stack Overflow用户
提问于 2019-09-20 19:27:39
回答 2查看 1.5K关注 0票数 1

我有一个JavaEE应用程序,我正在尝试使用cache2k和jCache来缓存一些函数的结果。当我调用带有@CacheResult注解的方法时,什么也没有发生。我被卡住了,不知道哪里出了问题。

我将这些依赖项添加到我的pom文件中:

代码语言:javascript
复制
       <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添加到我的类路径中。

代码语言:javascript
复制
<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)注册了侦听器,以便进行调试。

代码语言:javascript
复制
@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操作。

代码语言:javascript
复制
@Inject
@NamedCache(name="testCache")
private Cache<Object, Object> testCache;

但是,当我用@CacheResult注释某个方法时,什么也没有发生。

代码语言:javascript
复制
@CacheResult(cacheName="testCache")
public Long simpleMethod(@CacheKey String key) {
    return Long.valueOf(999);
}

该方法的结果不在缓存中。我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2019-09-21 17:58:50

从你的代码中,我不能100%确定发生了什么。请注意,注释RI并没有直接使用String作为键,而是将所有内容包装到另一个对象中。请参阅:EE8 JCache annotation CacheResult doesn't work

如果这个提示没有帮助,请添加如何检查缓存内容或添加示例输出。

票数 0
EN

Stack Overflow用户

发布于 2019-09-23 18:30:03

您需要在beans.xml中注册CacheResultInterceptor。类似于:

/META-INF/beans.xml

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58027507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档