开发者

How do I determine programmatically if ehcache is running?

开发者 https://www.devze.com 2023-02-03 18:30 出处:网络
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 an

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 whet开发者_开发技巧her 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.


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.


I implemented this in that way:

    public boolean areCachesDefined() {
        return this.cacheManagers.stream()
                .anyMatch(cacheManager -> cacheManager.getCacheNames().iterator().hasNext());
    }

where cacheManagers is a collection with cache handlers per cache type (for example ehcache)


Solution by @mglauche is pretty good. Additionally during startup you can search if your logs are printing following :

o.s.c.ehcache.EhCacheManagerFactoyBean : Initializing EhCache CacheManager
0

精彩评论

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

关注公众号