EHCache, how to check something in the cache or not?

Is there a way to check if an object is inside an EHCache managed cache?

The problem I am facing is that I implemented a method that retrieves a single value from the database (find (key) method). The result of this search method is nicely cached by EHCache, but now I want to reduce the number of sql queries that occur when the method is called several times.

So, for this we have introduced a new method, which takes a list of keys as an argument, but since the argument is different for each method, calling EHCache does a poor job of caching the results. EHCache used the method parameters as a cache entry point.

Therefore, I would like to rebuild some things. The idea was that I accept the arguments in the find (list of keys) method, execute a large sql query, and then fill the results inside the cache, I do not wrap it around, but after writing it it looks like manually changing the cache also does not go .

Any insight or tips appreciated!

+7
source share
3 answers

maybe isKeyInCache ?

+2
source

Using jmx, you can access hibernation statistics + ehcache statistics, etc. EhcacheHibernateMBean is the main interface that provides the whole API through jmx. It basically extends two interfaces - EhcacheStats and HibernateStats. And as the name suggests, EhcacheStats contains methods related to Ehcache and HibernateStats related to Hibernate. You can see that cache hit / miss / bet changes the values ​​of configuration items dynamically - for example, maxElementInMemory, TTI, TTL, enable / disable statistics collection, etc. And much more. This can be achieved in your application by overriding the buildSessionFactory () method on LocalSessionFactoryBean, adding tc.active as the "true" System property when the second level cache is enabled in the Hibernate configuration

@Override protected SessionFactory buildSessionFactory() throws Exception { Properties properties = this.getHibernateProperties(); String secondLevelCache = (String) properties .get("hibernate.cache.use_second_level_cache"); if (secondLevelCache.equals("true")) { System.setProperty("tc.active", "true"); } return super.buildSessionFactory(); } 

No, when you access your application through JMX, go to the Mbeans tab, on the left go to net.sf.ehcache.hibernate -> net.sf.ehcache.Cachemanager @ ..

Under this, go to the attributes. Click on the attributes and on the right hand side, check RegionCacheAttriutes.

enter image description here

Note : the view has changed using JDK1.7. After logging in to the JMX console, go to net.sf.ehcache.hibernate under the Mbeans tab. Click on CacheRegionStats. By clicking on it, the screen on the right will open. Double click on the top and it will display the table navigation as shown below. You will need to navigate the table navigation to find the amount of the object you are interested in.

+3
source

Have you looked at SelfPopulatingCache and or BlockingCache ? They will block all secondary requests after starting db recovery until the cache is full.

0
source

All Articles