简体   繁体   中英

Ehcache always returns null

I'm using Ehcache 2.6.8 as part of a Spring Boot application as the second-level cache for Hibernate 4.3.11. I have a JMX MBean that allows me to inspect the contents of the cache ( here if you are interested ).

First, I use the CacheManager name to get a list of caches. That works fine. I can return the cache with cacheManager.getEhcache(cacheName) . Then I call cache.getKeys() . This works fine, the cacheName is "com.myapp.DomainObjectName" and the keys are listed as "com.myapp.DomainObjectName#1", "com.myapp.DomainObjectName#2", etc.

But then I try to access one of the objects, by calling cache.get("com.myapp.DomainObjectName#1") , using one of the keys that the cache itself told me was present. Yet, the result is always null.

Why is that? Why can I not find an object associated with a key that the cache itself, just moments before, said was present?

PS, probably not relevant, but I'm using JGroups to distribute the cache in production. However, I see the same behavior even on my laptop.

The fact that you can find keys indicate that there is content in your cache.

From reading your question, you are wrongly interpreting the keys as being String I believe. What JMX shows you is the toString() of the key and thus using that String as the key returns you nothing.

You need a way to use directly the object in the get(key) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM