ehcache / ehcache-jcache

The Ehcache 2.x implementation of JSR107 (JCACHE)
Other
91 stars 48 forks source link

Arithmetic exception in JCacheStatistics #7

Closed JensN closed 10 years ago

JensN commented 12 years ago

The two methods getCacheMissPercentage() and getCacheHitPercentage() throw an ArithmeticException (dividing by zero) if you want to print statistics after the cache just started (hit + miss count = 0).

Workarounds are:

@Override public float getCacheMissPercentage() { long sum = this.statistics.getCacheHitCount() + this.statistics.getCacheMissCount(); if (sum <= 0) { return 0; } return (float) (this.statistics.getCacheMissCount() / ((double)sum)) * 100; }

respectively

@Override public float getCacheHitPercentage() { long sum = this.statistics.getCacheHitCount() + this.statistics.getCacheMissCount(); if (sum <= 0) { return 0; } return (float) ((this.statistics.getCacheHitCount() / ((double)sum)) * 100); }

ryangardner commented 12 years ago

Thanks for bringing this up. since it's a float, would returning Float.POSITIVE_INFINITY be more appropriate?

something like:

    public float getCacheMissPercentage() {
        if (statistics.getCacheHitCount() == 0.0 && statistics.getCacheMissCount() == 0.0) {
            return Float.POSITIVE_INFINITY;
        }
        return (statistics.getCacheMissCount() / (statistics.getCacheHitCount() + statistics.getCacheMissCount()));
    }

(and something similar for the cache miss percentage)