ehcache / ehcache3

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

ehcache 3 xml that works with JSR107 does not work with XmlConfiguration #3228

Closed subijayb closed 5 months ago

subijayb commented 5 months ago

Hi,

In my project we are upgrading ehcache 2 to ehcache 3. In order to understand the ehcache 3 and JSP107 implementation we were going though the samples https://github.com/ehcache/ehcache3-samples

I have a doubt. If I add <expiry> under <cache> in the XML, why does it not work with org.ehcache.xml.XmlConfiguration. If we have this XML

<ehcache:cache alias="basicCache">
 .......
   <ehcache:expiry>
        <ehcache:ttl unit="seconds">120</ehcache:ttl>
    </ehcache:expiry>
</ehcache:cache>

And when we run BasicXML class, The following code throws exception

Configuration xmlConfig = new XmlConfiguration(BasicXML.class.getResource("/ehcache.xml"));

Exception is ehcache3-samples/basic/target/classes/ehcache.xml; lineNumber: 15; columnNumber: 21; cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.ehcache.org/v3":expiry}'. One of '{"http://www.ehcache.org/v3":heap-store-settings, "http://www.ehcache.org/v3":disk-store-settings, "http://www.ehcache.org/v3":service-configuration}' is expected.

While, it works when JSR107 is used . This code works

String xmlClassPath = System.getProperty("jsr107.config.classpath", "ehcache-jsr107-simple.xml");
CachingProvider` cachingProvider = Caching.getCachingProvider();

try (CacheManager cacheManager = cachingProvider.getCacheManager(
      Thread.currentThread().getContextClassLoader().getResource(xmlClassPath).toURI(),
      Thread.currentThread().getContextClassLoader())) {
} catch (....) {}

Why is the difference? Can someone please explain?

subijayb commented 5 months ago

I was having <expiry> after <resource> tag. Thats why it was not working After i added <expiry> before <resource> , it worked. Below is working sample

    <cache alias="myCacheName">
        <expiry>
            <none />
        </expiry>
        <resources>
            <heap unit="entries">30</heap>
        </resources>

    </cache>