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

perhaps isKeyInCache?


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.