开发者

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开发者_开发百科!


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.

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

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.


Have you looked at SelfPopulatingCache and or BlockingCache? They will block all secondary requesters after a db retrieve is initiated until the cache is populated

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜