Layer 2 sleeping cache via ehcache with Terracotta - not caching at all?

I have this spring / hibernate project which I am trying to add a second level cache to sleep mode through ehcache and terracotta. It seems that everything is in order, I even see in the terracota console entries for the objects that I am trying to cache. But based on statistics and a log from the database, nothing is cached at all!

The load factor of the load is 0%, the load statistics is 0. What am I doing wrong?

Here is what I did, I added the necessary banks through maven.

<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-terracotta</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.terracotta</groupId> <artifactId>terracotta-toolkit-1.5-runtime</artifactId> <version>4.2.0</version> </dependency> 

Hibernate property changed to enable second level cache

 <property name="hibernateProperties"> <props> ... <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_structured_entries">true</prop> <prop key="hibernate.cache.generate_statistics">true</prop> </props> </property> 

Added @Cache annotation for my test object

 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class User implements java.io.Serializable { ... } 

Here is my extremely simple ehcache.xml (I also tried setting a cache entry for my object with the same result)

 <?xml version="1.0" encoding="UTF-8"?> <ehcache > <defaultCache maxElementsInMemory="10000" eternal="false" maxEntriesLocalHeap="10" timeToIdleSeconds="120" timeToLiveSeconds="120"> <terracotta/> </defaultCache> <terracottaConfig url="localhost:9510"/> </ehcache> 

And after I started my terracotta server and ran my test code

 @Test @Transactional @Rollback(false) public void testCache() { long start = System.currentTimeMillis(); List<User> list = userRepository.listAll(0, 100); long end = System.currentTimeMillis(); log.info("Total time "+(end-start)); assertNotNull(list); assertThat(list.size(), is(100)); for (int i=0; i<100; i++) { long start2 = System.currentTimeMillis(); list = userRepository.listAll(0, 100); long end2 = System.currentTimeMillis(); log.info("Total time 2 "+(end2-start2)); } assertNotNull(list); assertThat(list.size(), is(100)); } 

My log displays 100 SQL that should not occur. It also shows a hit rate of 0%.

Here are some screenshots from the terracotta console during my test.

What is this last piece that I need for this to work?

2nd level cache statisticsehcache overviewEntity statistics

+4
source share
1 answer

Found a solution to the problems that I experimented with, here are the details:

  • Statistics were not set due to an invalid key in the sleep mode properties

Use this (pay attention to .cache. )

 <prop key="hibernate.generate_statistics">true</prop> 

instead

 <prop key="hibernate.cache.generate_statistics">true</prop> 
  • Requests were not cached because I did not use ".setCachable (true)" for the method that was responsible for listing / loading objects.
+3
source

Source: https://habr.com/ru/post/1412662/


All Articles