¿Cómo determino programáticamente si ehcache se está ejecutando?

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 whether 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.

preguntado el 08 de enero de 11 a las 22:01

EhCache doesn't "run", it's a library. What are you asking? -

4 Respuestas

Intente algo como esto:

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());
        }
}

Respondido 01 Oct 15, 13:10

It prints Got: DEFAULT and "cachename" - net.sf.ehcache.statistics.StatisticsGaterwat@1y6788898 Why the cachemanager name is default? is it alright? - Plato

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.

Respondido el 09 de enero de 11 a las 03:01

I was hoping to avoid this since I haven't downloaded it, etc... It seems I'm using version 3.3.1 of ehcache. However, I haven't been able to find the source code. So you don't think there is a way to setup some kind of logging so that I know it even loaded and configured ehcache? - Andrés

@Andrew - You don't need the EHCache source to see if it shows up in a stack in the debugger. You just need a breakpoint. - rfeak

I implemented this in that way:

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

dónde cacheManagers is a collection with cache handlers per cache type (for example ehcache)

respondido 04 mar '19, 13:03

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

Respondido 06 Abr '21, 10:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.