Reputation: 26723
I am trying to write a class that would trace all Ehcache GET requests. For the performance obsessed - this would only be switched on for debugging purposes.
I can see following three options available:
1) write an implementation of net.sf.ehcache.event.CacheEventListener
. This would be my preferred way, but it only has PUT/REMOVE/etc. hooks. No GET. Doh!
2) write an implementation of net.sf.ehcache.statistics.CacheUsageListener
. The problem with this approach is that this interface is more designed for statistics and does not even provide access to things like current cache key/element, so I would have to do horrible hacks (think: sharing state through ThreadLocal
) to achieve what I want. Yuck!
3) write an Ehcache wrapper, and channel all requests through it. Quite painful as we use Ehcache in different ways (with Hibernate and without), meaning I would have to write different wrappers for all these different cases. Adds maintenance pains and isn't really precise as it is not possible to know, for example, if a GET operation has hit a stale entry or not.
Are there any other options I missed?
Upvotes: 1
Views: 2042
Reputation: 1250
A fourth option would be to enable debug level logging for net.sf.ehcache.Cache and enable cache statistics for the caches you want to trace the information. You'd then get "Cache: " + getName() + " store hit for " + key" or "configuration.getName() + " cache hit, but element expired"" log statements... So for misses as well.
Upvotes: 1