Reputation: 95
I've set up a cache in one of my projects, along with the custom health indicator as below:
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator
{
@Override
protected void doHealthCheck(Health.Builder builder)
{
CacheManager cacheManagerInstance = CacheManager.getInstance();
Ehcache cache = cacheManagerInstance.getCache("myCache");
builder.up()
.withDetail("Cache Size", cache.getSize())
.withDetail("Cache Hit Count", cache.getStatistics().cacheHitCount())
.withDetail("Cache Miss Count", cache.getStatistics().cacheMissCount())
.withDetail("Cache Put Count", cache.getStatistics().cachePutCount())
.withDetail("Cache Remove Count", cache.getStatistics().cacheRemoveCount())
.withDetail("Cache Expired Count", cache.getStatistics().cacheExpiredCount());
}
When I run it locally and go to localhost:8080/actuator/health all the other statistics update but the hits doesn't.
{"status":"UP","details":{"Cache Size":9,"Cache Hit Count":0,"Cache Miss Count":9,"Cache Put Count":9,"Cache Remove Count":0,"Cache Expired Count":0}}
My code accessing the cache in my other project is:
Cacheable cachedResult = CacheManager.getInstance().getCache(request);
if (cachedResult != null)
{
CustomResponse response = (CustomResponse) cachedResult.getObject();
return response;
}
I can confirm that this block is entered and that there actually is data in the cache.
I have no idea why the hits statistic isn't being updated. Any help would be appreciated!
Upvotes: 0
Views: 353
Reputation: 95
Turns out my objects were not being serialized correctly, so when it was searching for them in the cache they were not being found.
Upvotes: 1