Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When/How do I set Ehcache used by Hibernate size programmatically

I have enabled 2nd level caching in Hibernate 4.3.11 by adding:

 config.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
 config.setProperty("hibernate.cache.use_second_level_cache", "true");

to my Hibernate Config.

This to my pom.xml (Not sure if necessary for pom definition to be this awkward)

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>4.3.11.Final</version>
      <exclusions>
          <exclusion>
              <groupId>net.sf.ehcache</groupId>
              <artifactId>ehcache-core</artifactId>
          </exclusion>
      </exclusions>
      </dependency>
      <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>2.7.0</version>
      </dependency>

and this to the class I want to cache

@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

But how do I configure the cache size in code when the database is created, its not practical for me to use Xml file that just adds complication to the build process I would much prefer to do in code.

Update, after creating database from Hibernate, I find the Caches are already created

CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();
for(String cacheName:cacheNames)
{
    MainWindow.logger.severe("CacheName:"+cacheName);
    Cache cache = CacheManager.getInstance().getCache(cacheName);
    cache.getCacheConfiguration().setMaxEntriesInCache(1000);
    cache.getCacheConfiguration().setLogging(true);
}

but how can I affect how they are created or does modifying values like i have done is enough to update. When I run I see no debugging outout or anything to indicate the cache is being used.

like image 812
Paul Taylor Avatar asked Oct 15 '18 19:10

Paul Taylor


People also ask

How do I set up Ehcache?

Ehcache can be configured in two ways: The first way is through Java POJO where all configuration parameters are configured through Ehcache API. The second way is configuration through XML file where we can configure Ehcache according to provided schema definition.

What is difference between Ehcache and Redis cache?

You can think Redis as a shared data structure, while Ehcache is a memory block storing serialized data objects. This is the main difference. Redis as a shared data structure means you can put some predefined data structure (such as String, List, Set etc) in one language and retrieve it in another language.


1 Answers

You could subclass org.hibernate.cache.ehcache.EhCacheRegionFactory and perform any cache configuration manually, and then tell Hibernate to use your custom cache factory with:

Configuration.setProperty("hibernate.cache.region.factory_class", "my.cache.FactoryClass");

See: http://www.ehcache.org/documentation/2.7/integrations/hibernate.html#set-the-hibernate-cache-provider-programmatically-

like image 136
marcprux Avatar answered Oct 13 '22 16:10

marcprux