Search code examples
javacachingjakarta-eeehcacheehcache-3

How to put all the Cache names and their data in List<> when cache is created?


I have used Ehcache 3 and I am new to Ehcache 3 and I have a cache Manager given as:

 private CacheManager cacheManager;

Now when the object is created cacheManager is initialized:

public ListPlanImpl() {
        System.out.println("constructore being initalized");
        System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
        cacheManager = CacheManagerBuilder
                .newCacheManagerBuilder().build();
        cacheManager.init();


    }

After the cacheManager is initialized,This is my main class where all actions of getting and putting in cache is happening.I have inserted several data into different caches such as:

     @Stateless
        public class ListPlanImpl implements CachePlan,ListPlan {

         private static final String CACHE_OPERATING_PARAMETER = "cache_key_operating_parameter";
            private static final String CACHE_SECURITY_PARAMETER = "cache_security";
      private static Cache<String, GenericClassForList> operatingParametersCache;
        private static Cache<String, GenericClassForList> securitiesTradingParameterCache;

         public void putInCache() throws ExecutionException, InterruptedException {
                System.out.println("putting in list");

                this.operatingParametersCache = cacheManager
                        .createCache("cacheOfOperatingParameters", CacheConfigurationBuilder
                                .newCacheConfigurationBuilder(
                                        String.class, GenericClassForList.class,
                                        ResourcePoolsBuilder.heap(1000000000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
                                        TimeUnit.SECONDS))));

                operatingParametersCache.put(CACHE_OPERATING_PARAMETER, new GenericClassForList(this.operatingParamService.getOperatingParamDTO()));


                this.securitiesTradingParameterCache = cacheManager
                        .createCache("cacheOfSecurityTrading", CacheConfigurationBuilder
                                .newCacheConfigurationBuilder(
                                        String.class, GenericClassForList.class,
                                        ResourcePoolsBuilder.heap(1000000000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
                                        TimeUnit.SECONDS))));
  securitiesTradingParameterCache.put(CACHE_SECURITY_PARAMETER, new GenericClassForList(this.securitiesTradingParamsService.getSecuritiesTradingParamDTO()));

            }
        }

I want a separate function which will return me all the caches name and count of the total no of the data in cache,So that I can show in UI that the cache which contains data.I searched for the problem but I didn't found the solution.


Solution

  • Cache implements Iterable<Cache.Entry<K,V>> so you can iterate over the entries to get the keys and values for each cache, e.g. like this:

    for( Cache.Entry<String, GenericClassForList> entry : operatingParametersCache) { 
      String key = entry.getKey(); 
      GenericClassForList value = entry.getValue(); 
      //if counting just increasing a counter might be sufficient, 
      //otherwise use the key and value as needed 
    }
    

    Since you already have references to the caches just apply that method to each individual cache.

    If you don't have the references handy you could keep some registry on the used aliases for the caches (you're providing the aliases so you could use those to get the caches from the cache manager). If you can't do that either you might need to track the aliases fed to the cache manager, e.g. by wrapping it with a delegate.