1

I have JavaEE application and I'm trying to use cache2k with jCache to cache results of some functions. When I call methods annotated with @CacheResult nothing happens. I'm stuck and don't know what's wrong.

I added these dependencies to my pom file:

       <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>

I added cache2k.xml to my classpath.

<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>

I have producer method for cache injecting. There I register listener for all cache events (onCreated, onUpdated, onRemoved, onExpired) for debug purposes.

@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;
}

I can inject cache object in my service classes. I can use this cache and do get and put operations with it.

@Inject
@NamedCache(name="testCache")
private Cache<Object, Object> testCache;

But when I annotate some method with @CacheResult nothing happens.

@CacheResult(cacheName="testCache")
public Long simpleMethod(@CacheKey String key) {
    return Long.valueOf(999);
}

Result of the method is not in the cache. What am I doing wrong?

paxrex
  • 41
  • 1
  • 5

2 Answers2

0

From your code I am not 100% sure what is happening. Please mind that the annotations RI is not using the String as a key directly but wraps everything into another object. See: EE8 JCache annotation CacheResult doesn't work

If this hint doesn't help, please add how you check the cache contents or add example output.

cruftex
  • 5,545
  • 2
  • 20
  • 36
  • Thank you for the hint. I will read it thoroughly. I do not have the code that check cache content. I simply called the method few times in a row and logged the calls. The method is called every time. I also have listener for onCreated cache event and this event is not triggered when I use _@CacheResult_ annotation. But when I inject cache and use put method I can see in log that event is triggered. – paxrex Sep 23 '19 at 09:42
0

You need to register the CacheResultInterceptor in beans.xml. Something like:

/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>
MrAndersen
  • 11
  • 3