I am trying to migrate my project code from OSCache to EhCache.
We have used OSCache not only as a second-level Hibernate cache provider but also to store other obje
You can try having one single cache and adding decorators around it. The decorators can have names matching your region names so that hibernate can use those caches but those decorators would be using the same cache underneath. So theres only one cache config to manage. You can achieve this by implementing Custom cache decorators and set up the names of your decorated caches.
You can have ehcache.xml something like this:
<defaultCache maxElementsInMemory="10000" eternal="false"
overflowToDisk="false"/>
<cache name="singleSharedCache" maxElementsInMemory="2000"
eternal="false" overflowToDisk="false">
<cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
properties="name=org.hibernate.tutorial.domain.Person" />
<cacheDecoratorFactory class="com.xyz.util.CustomEhcacheDecoratorFactory"
properties="name=org.hibernate.tutorial.domain.Event" />
</cache>
The "com.xyz.util.CustomEhcacheDecoratorFactory" is a custom ehcache decorator factory class which is used to create the decorated ehcaches. You can use the "properties" attribute to set up the decorated ehcache in any way you want, here you only use a name property to configure the name of the new decorated ehcache. All other operations can be delegated to the underlying cache.
Providing one custom cache decorator that would work for this use-case here, it reuses the EhcacheDecoratorAdapter that comes in the ehcache jar and just overrides getName(). EhcacheDecoratorAdapter delegates all operations to an underlying ehcache which you pass in the constructor:
package com.xyz.util; import java.util.Properties; import net.sf.ehcache.Ehcache; import net.sf.ehcache.constructs.CacheDecoratorFactory; import net.sf.ehcache.constructs.EhcacheDecoratorAdapter; public class CustomEhcacheDecoratorFactory extends CacheDecoratorFactory { public Ehcache createDecoratedEhcache(final Ehcache cache, final Properties properties) { return new EhcacheDecoratorAdapter(cache) { private final String name = properties.getProperty("name"); public String getName() { return name; } }; } public Ehcache createDefaultDecoratedEhcache(final Ehcache cache, final Properties properties) { return new EhcacheDecoratorAdapter(cache) { private final String name = properties.getProperty("name"); public String getName() { return name; } }; } }