1

I am working with infinispan cache (7.1.0.Final) in a maven application running on wildfly 10.1.0.Final.

Caching is working fine, I am using the automatic caching approach with @CacheResult, @CacheRemove, @CacheRemoveAll annotations and defined the interceptors in beans.xml (CacheResultInterceptor, CachePutInterceptor, CacheRemoveAllInterceptor, CacheRemoveEntryInterceptor).

I did not find a way to retrieve number of entries in javax.cache.Cache. Returned cache is instance of org.infinispan.jcache.JCache, no way either to get the count.

I thought as another option to make a custom entry listener and perform the counting on onCreated, onRemoved and onExpired entry events. It sounds a bit dirty ... I could not test that yet, but I think there should be a clever solution to this.

Any help is appreciated.

Best, Juan

  • 2
    You can call `unwrap` passing the provider specific class name expected and from that you should be able to get size. E.g. `org.infinispan.Cache unwrap = jcache.unwrap(org.infinispan.Cache.class);` – Galder Zamarreño Feb 15 '18 at 16:31
  • Thanks for the reply. I tested that and could get the underlying "org.infinispan.Cache.class". Although invoking `unwrap.size()` always returns 0. I also tested `unwrap.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).size()` without success. – Juan Pablo Perata Feb 15 '18 at 19:13
  • 1
    You should probably skip `getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL)`. Just `unwrap.size()`. – Sebastian Łaskawiec Feb 16 '18 at 10:53
  • Thanks @altanis. The `unwrap.size()` was my first approach as recommended by Galder. But always returns 0, despite having elements in cache. I would like to understand if I am missing something or if I need to "force" cache to return the number of entries. – Juan Pablo Perata Feb 16 '18 at 12:54
  • If I do `Cache sampleCache = manager.getCache("sampleCache"); sampleCache.put("sample key", "sample value");` and then `unwrap.size()` it returns 1, which is correct. My question is: why this do not work with @CacheResult annotations? – Juan Pablo Perata Feb 16 '18 at 17:41
  • Hmmmm, strange. Maybe the jcache reference you're using belongs to a different cache than the one used by @CacheResult? Can you post your code? – Galder Zamarreño Feb 19 '18 at 14:10
  • web.war with dependency custom.jar. _custom.jar_ defines (a) managed bean A with method `@CacheResult(name="sampleCache")String getName(int id){...}`; (b) class B with a static method `startCache(){Caching.getCachingProvider().getCacheManager().createCache("sampleCache", new MutableConfiguration<>());}`; (c) singleton startup bean, it invokes A.startCache. **web.jar** defines managed bean W, it injects A and invokes A.getName If `Caching.getCachingProvider().getCacheManager().getCache("sampleCache").unwrap(org.infinispan.Cache.class).size()` in A, the size is ok. The same in W returns 0. – Juan Pablo Perata Feb 19 '18 at 21:52
  • To explain myself a bit more, what I tried to say is that a managed bean is injected in a class in web.war and getting an instance of Cache there return size 0, while doing the same in custom.jar returns the size as it should be. – Juan Pablo Perata Feb 19 '18 at 21:58
  • I really appreciate your help. I would like to point out I noticed to have another method: `@CacheResult(name="sampleCache")Integer getIdentifier(String key){...}` in bean **A**, and this method gets executed in another Startup bean before the cache is created with _startCache_, What happens in this case? Is it possible that two cache managers get created with different name and class loaders? I see the infinispan logs and the cache manager is created with default URI and class loader. – Juan Pablo Perata Feb 19 '18 at 22:15

0 Answers0