1

In earlier versions of Ehcache you where able to define a default-cache configuration, which will be applied to caches created programmatically. Since Ehcache3 they changed the configuration xsd quite a lot and there doesn't seem to be an equivalent to defaultCache anymore - or at least, I cannot find it anywhere.

For example old configuration:

<ehcache
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="false">

    <defaultCache 
        maxElementsInMemory="10000" 
        eternal="false"
        timeToLiveSeconds="120"
        overflowToDisk="false" />
</ehcache>

I tried it with the following configuration, but I don't know if that is working or not or if I can just remove and ignore it. I guess I can ignore it because when I boot the application I don't get any warnings that ehcache cannot find a configuration for a specific cache.

New configuration:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

    <cache alias="default">
        <expiry>
            <ttl>120</ttl>
        </expiry>
        <heap>10000</heap>
    </cache>
</config>

The reason why I'm using alias="default" is because in this documentation: ehcache.xml they mentioned that the internal name for defaultCache is "default". Quote:

Default Cache configuration. These settings will be applied to caches
created programmatically using CacheManager.add(String cacheName).
This element is optional, and using CacheManager.add(String cacheName) when
its not present will throw CacheException

The defaultCache has an implicit name "default" which is a reserved cache name.

So the question is, is there an equivalent for defaultCache in Ehcache3 or can I just remove it?

Sepultura
  • 997
  • 1
  • 9
  • 28

1 Answers1

1

I tried your configuration with <cache alias="default">, and it didn't work for Ehcache 3.x. Ehcache still created new cache for missing cache with random default configuration.

What I found out is that they removed the feature of defaultCache in Ehcache 3.x. However, there is a feature of Ehcache 3.x JSR-107 provider and its configuration that allows the same function as defaultCache. You can see here for more information.

Below is my Spring Boot configuration that allows default configuration for missing cache creation with Ehcache 3.x:

pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jcache</artifactId>
</dependency>

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

application.properties

spring.jpa.properties.hibernate.cache.use_second_level_cache = true
spring.jpa.properties.hibernate.cache.use_query_cache        = true
spring.jpa.properties.hibernate.cache.region.factory_class   = org.hibernate.cache.jcache.JCacheRegionFactory
spring.jpa.properties.hibernate.javax.cache.provider         = org.ehcache.jsr107.EhcacheCachingProvider
spring.jpa.properties.hibernate.javax.cache.uri              = classpath:ehcache.xml
spring.jpa.properties.javax.persistence.sharedCache.mode     = ENABLE_SELECTIVE
spring.jpa.properties.hibernate.generate_statistics          = true

src/main/resources/ehcache.xml

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>
    
    <service>
        <jsr107:defaults default-template="myDefault">
        </jsr107:defaults>
    </service>

    <cache-template name="myDefault">
        <key-type>java.lang.Object</key-type>
        <value-type>java.lang.Object</value-type>
        <expiry>
            <ttl unit="seconds">300</ttl>
        </expiry>
        <resources>
            <heap unit="entries">1000</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache-template>
</config>
Khoa Le
  • 95
  • 1
  • 1
  • 11