I have a large java application that is configured to use JPA and Hibernate. It is also supposedly configured to use ehcaching for both entity and query caching. However, I have sql logging turned on and no entities are being cached. All of the entity queries are happening on every request.
How can I determine at runtime if it is even running ehcache and whether it thinks an entity should be cacheable?
I didn't write this app so I'm stuck a bit here.
It uses declarations for the caching on the classes.
It is correctly using all the other declarations for Hibernate to perform the read/write operations.
We can use Maven to start this app by running mvn spring-boot:run. Then open up a browser and access the REST service on port 8080. The log message in the square method of NumberService isn't being invoked. This shows us that the cached value is being used.
ehcache. hibernate under Mbeans tab. Click on the CacheRegionStats Clicking on it will open bring up the screen on the right. Double click on top section and it brings up the tabular navigation as shown below.
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.
Try something like this:
List<CacheManager> tempManagers = CacheManager.ALL_CACHE_MANAGERS;
System.out.println("# of CMs : " + tempManagers.size());
for (CacheManager tempCM : tempManagers) {
System.out.println("Got: " + tempCM.getName());
String[] cacheNames = tempCM.getCacheNames();
for (int i = 0; i < cacheNames.length; i++) {
String cacheName = cacheNames[i];
System.out.println(cacheName+" - "+ tempCM.getEhcache(cacheName).getStatistics().toString());
}
}
The short answer - a debugger.
Put a breakpoint where you load an entity and follow it down the stack. See if it ever even attempts to get the object from EHCache. Also, check to see if it tries to put it in the cache after it fetches it from the DB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With