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?