ehcache / ehcache3

Ehcache 3.x line
http://www.ehcache.org
Apache License 2.0
2.02k stars 580 forks source link

Migrating XML configuration between 2.x and 3.x - missing settings like memoryStoreEvictionPolicy #3248

Closed jivkodobrev closed 1 month ago

jivkodobrev commented 1 month ago

I am migrating from Spring 2.x / EHCache 2.x to Spring 3.x / EHCache 3.x We have an XML configuration file with about 15 caches defined in an XML file

I am unable to find matching XML settings for items like

Example cache definition I am trying to upgrade:

    <cache name="indirectly_related_offers"
           maxElementsInMemory="500" timeToLiveSeconds="1200" timeToIdleSeconds="1200"
           eternal="false" memoryStoreEvictionPolicy="LRU" overflowToDisk="false"/>

This is the XSD of ehcache 3.x I am looking at: https://www.ehcache.org/documentation/3.8/xsds.html#core

Can you provide details on how to author the 3.x XML config file to preserve the 2.x cache settings? E.g. LRU/LFU policy?

In addition, migration to EHCache 3.x is clumsy. In 2.x EhCacheCacheManager implemented Spring's CacheManager interface and I was able to use it in our configuration side-by-side with Redis in Spring. Now I have a clumsy code like this:

   public CacheManager ehCacheManager() throws URISyntaxException {
        ClassLoader classLoader = getClass().getClassLoader();
        URI ehcacheURI = Objects.requireNonNull(classLoader.getResource("ehcache.xml")).toURI();
        return new JCacheCacheManager(
            Caching.getCachingProvider(EhcacheCachingProvider.class.getName())
                .getCacheManager(ehcacheURI, classLoader));
    }

Unless you can provide a better approach?

TL;DR - my primary issue here is to define xml configuration in EHCache 3.x to match my 2.x XML configuration.

Thank you in advance

chrisdennis commented 1 month ago
<cache name="indirectly_related_offers"
           maxElementsInMemory="500" timeToLiveSeconds="1200" timeToIdleSeconds="1200"
           eternal="false" memoryStoreEvictionPolicy="LRU" overflowToDisk="false"/>

would map to:

<cache alias="indirectly_related_offers">
  <resources>
    <heap unit="entries">500</heap>
  </resources>
  <expiry>
    <ttl unit="minutes">20</ttl>
  </expiry>
</cache>

Couple of things to note here:

  1. overflowToDisk="false" translates to nothing since in Ehcache 3 only the resources you declare will be used.
  2. eternal="false" is redundant in your Ehcache 2 config since you specify a TTI and TTL.
  3. The memoryStoreEvictionPolicy is not configurable in Ehcache 3.
  4. TTI and TTL are not supported at the same time, and TTI is considered "informally deprecated": https://github.com/ehcache/ehcache3/issues/1097