Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EHCache how to check if something is in the cache or not?

Is there a way of checking if an object is inside an EHCache managed cache?

The challenge I face is that I have implemented a method that retrieves a single value from the database (a find(key) method). The result of that find method is nicely cached by EHCache, but now I want to reduce the number of SQL queries that result from calling the method several times.

So to achieve this we implemented a new method which as argument takes a list of keys, but as the argument is different for every method call EHCache does a bad job on caching the results. EHCache used the method parameters as entry point to the cache.

So I would like to re-engineer some stuff. The idea was that I take the arguments in the find(list of keys) method, execute a large SQL query and then stuff the results inside the cache, I have not wrapped my head around it, but after writing this down it feels like manually modifying the cache is also a no go.

Any insight or hints are appreciated!

like image 327
Marco Avatar asked Jun 22 '11 12:06

Marco


2 Answers

perhaps isKeyInCache?

like image 172
jtahlborn Avatar answered Oct 01 '22 03:10

jtahlborn


It is possible to access the hibernate statistics + ehcache stats etc via jmx. EhcacheHibernateMBean is the main interface that exposes all the API's via jmx. It basically extends two interfaces -- EhcacheStats and HibernateStats. And as the name implies EhcacheStats contains methods related with Ehcache and HibernateStats related with Hibernate. You can see cache hit/miss/put rates, change config element values dynamically -- like maxElementInMemory, TTI, TTL, enable/disable statistics collection etc and various other things. This can be achieved in your application by overriding buildSessionFactory() method on LocalSessionFactoryBean by adding tc.active as "true" System property when second level cache is enabled in 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 via JMX, go to tab Mbeans , on left go to net.sf.ehcache.hibernate --> net.sf.ehcache.Cachemanager@..

Under this go to attributes. Click on attributes and on right side, inspect RegionCacheAttriutes.

enter image description here

Note : The view has changed with JDK1.7 . After logging into JMX Console, navigate to net.sf.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 will have to navigate in the tabular navigation to find the count of any object you are interested in.

like image 41
vsingh Avatar answered Oct 01 '22 04:10

vsingh