1

I know how to create a CacheManager using an XMLConfiguration in org.ehcache:ehcache:3.8.1:

import org.ehcache.config.Configuration;
import org.ehcache.xml.XmlConfiguration;
import org.ehcache.config.builders.CacheManagerBuilder;
    .
    .
    .
    URL myUrl = CacheUtil.class.getResource("/my-config.xml");
    Configuration xmlConfig = new XmlConfiguration(myUrl);
    cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
    cacheManager.init();

I also know how to create a CacheManager with a StatisticsService:

StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
      .using(statisticsService)
      .build();
cacheManager.init();

But how do I create a CacheManager from an XMLConfiguration using a StatisticsService?

vicpermir
  • 3,544
  • 3
  • 22
  • 34
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202

2 Answers2

1

Here is a sample code that demonstrates the usage of basic JCache configuration APIs:

 CachingProvider provider = Caching.getCachingProvider();  
CacheManager cacheManager = provider.getCacheManager();   
MutableConfiguration<Long, String> configuration =
    new MutableConfiguration<Long, String>()  
        .setTypes(Long.class, String.class)   
        .setStoreByValue(false)   
        .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));  
Cache<Long, String> cache = cacheManager.createCache("jCache", configuration); 
cache.put(1L, "one"); 
String value = cache.get(1L); 
  1. Retrieves the default CachingProvider implementation from the application’s classpath. This method will work if and only if there is only one JCache implementation jar in the classpath. If there are multiple providers in your classpath then use the fully qualified name org.ehcache.jsr107.EhcacheCachingProvider to retrieve the Ehcache caching provider. You can do this by using the Caching.getCachingProvider(String) static method instead.

  2. Retrieve the default CacheManager instance using the provider.

  3. Create a cache configuration using MutableConfiguration

  4. with key type and value type as Long and String respectively…​

  5. configured to store the cache entries by reference(not by value)…​

  6. and with an expiry time of one minute defined for entries from the moment, they are created.

  7. Using the cache manager, create a cache named jCache with the configuration created in step <3>

  8. Put some data into the cache.

  9. Retrieve the data from the same cache.
Govind Prajapati
  • 208
  • 3
  • 10
0

There is a constructor inside the class EhcacheManager that takes 2 arguments:

public EhcacheManager(Configuration config, Collection<Service> services)

You could use it as follows:

URL myUrl = CacheUtil.class.getResource("/my-config.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);

StatisticsService statisticsService = new DefaultStatisticsService();
Set<Service> services = new HashSet<>();
services.add(statisticsService);

cacheManager = new EhcacheManager(xmlConfig, services);
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Once you've set this up, how do you actually get run-time statistics? Is this documented somewhere? – Paul Reiners Mar 03 '20 at 20:11
  • Try to call `getCacheStatistics` and `collectStatistics` on `statisticsService`: https://github.com/ehcache/ehcache3/blob/master/core/src/main/java/org/ehcache/core/spi/service/StatisticsService.java https://github.com/ehcache/ehcache3/blob/master/core/src/main/java/org/ehcache/core/statistics/CacheStatistics.java – Eng.Fouad Mar 04 '20 at 01:50