Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.cache;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
 
 
 import  javax.annotation.Nullable;

Statistics about the performance of a Cache. Instances of this class are immutable.

Cache statistics are incremented according to the following rules:

  • When a cache lookup encounters an existing cache entry hitCount is incremented.
  • When a cache lookup first encounters a missing cache entry, a new entry is loaded.
    • After successfully loading an entry missCount and loadSuccessCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
    • When an exception is thrown while loading an entry, missCount and loadExceptionCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
    • Cache lookups that encounter a missing cache entry that is still loading will wait for loading to complete (whether successful or not) and then increment missCount.
  • When an entry is evicted from the cache, evictionCount is incremented.
  • No stats are modified when a cache entry is invalidated or manually removed.
  • No stats are modified on a query to Cache.getIfPresent.
  • No stats are modified by operations invoked on the asMap view of the cache.

A lookup is specifically defined as an invocation of one of the methods LoadingCache.get(Object), LoadingCache.getUnchecked(Object), Cache.get(Object, Callable), or LoadingCache.getAll(Iterable).

Author(s):
Charles Fry
Since:
10.0
 
 public final class CacheStats {
   private final long hitCount;
   private final long missCount;
   private final long loadSuccessCount;
   private final long loadExceptionCount;
   private final long totalLoadTime;
   private final long evictionCount;

  
Constructs a new CacheStats instance.

Five parameters of the same type in a row is a bad thing, but this class is not constructed by end users and is too fine-grained for a builder.

 
   public CacheStats(long hitCountlong missCountlong loadSuccessCount,
       long loadExceptionCountlong totalLoadTimelong evictionCount) {
     checkArgument(hitCount >= 0);
     checkArgument(missCount >= 0);
     checkArgument(loadSuccessCount >= 0);
     checkArgument(loadExceptionCount >= 0);
     checkArgument(totalLoadTime >= 0);
     checkArgument(evictionCount >= 0);
 
     this. = hitCount;
     this. = missCount;
     this. = loadSuccessCount;
     this. = loadExceptionCount;
     this. = totalLoadTime;
     this. = evictionCount;
   }

  
Returns the number of times Cache lookup methods have returned either a cached or uncached value. This is defined as hitCount + missCount.
 
   public long requestCount() {
     return  + ;
  }

  
Returns the number of times Cache lookup methods have returned a cached value.
  public long hitCount() {
    return ;
  }

  
Returns the ratio of cache requests which were hits. This is defined as hitCount / requestCount, or 1.0 when requestCount == 0. Note that hitRate + missRate =~ 1.0.
  public double hitRate() {
    long requestCount = requestCount();
    return (requestCount == 0) ? 1.0 : (double / requestCount;
  }

  
Returns the number of times Cache lookup methods have returned an uncached (newly loaded) value, or null. Multiple concurrent calls to Cache lookup methods on an absent value can result in multiple misses, all returning the results of a single cache load operation.
  public long missCount() {
    return ;
  }

  
Returns the ratio of cache requests which were misses. This is defined as missCount / requestCount, or 0.0 when requestCount == 0. Note that hitRate + missRate =~ 1.0. Cache misses include all requests which weren't cache hits, including requests which resulted in either successful or failed loading attempts, and requests which waited for other threads to finish loading. It is thus the case that missCount >= loadSuccessCount + loadExceptionCount. Multiple concurrent misses for the same key will result in a single load operation.
  public double missRate() {
    long requestCount = requestCount();
    return (requestCount == 0) ? 0.0 : (double / requestCount;
  }

  
Returns the total number of times that Cache lookup methods attempted to load new values. This includes both successful load operations, as well as those that threw exceptions. This is defined as loadSuccessCount + loadExceptionCount.
  public long loadCount() {
  }

  
Returns the number of times Cache lookup methods have successfully loaded a new value. This is always incremented in conjunction with missCount, though missCount is also incremented when an exception is encountered during cache loading (see loadExceptionCount). Multiple concurrent misses for the same key will result in a single load operation.
  public long loadSuccessCount() {
    return ;
  }

  
Returns the number of times Cache lookup methods threw an exception while loading a new value. This is always incremented in conjunction with missCount, though missCount is also incremented when cache loading completes successfully (see loadSuccessCount). Multiple concurrent misses for the same key will result in a single load operation.
  public long loadExceptionCount() {
    return ;
  }

  
Returns the ratio of cache loading attempts which threw exceptions. This is defined as loadExceptionCount / (loadSuccessCount + loadExceptionCount), or 0.0 when loadSuccessCount + loadExceptionCount == 0.
  public double loadExceptionRate() {
    long totalLoadCount =  + ;
    return (totalLoadCount == 0)
        ? 0.0
        : (double / totalLoadCount;
  }

  
Returns the total number of nanoseconds the cache has spent loading new values. This can be used to calculate the miss penalty. This value is increased every time loadSuccessCount or loadExceptionCount is incremented.
  public long totalLoadTime() {
    return ;
  }

  
Returns the average time spent loading new values. This is defined as totalLoadTime / (loadSuccessCount + loadExceptionCount).
  public double averageLoadPenalty() {
    long totalLoadCount =  + ;
    return (totalLoadCount == 0)
        ? 0.0
        : (double / totalLoadCount;
  }

  
Returns the number of times an entry has been evicted. This count does not include manual invalidations.
  public long evictionCount() {
    return ;
  }

  
Returns a new CacheStats representing the difference between this CacheStats and other. Negative values, which aren't supported by CacheStats will be rounded up to zero.
  public CacheStats minus(CacheStats other) {
    return new CacheStats(
        Math.max(0,  - other.hitCount),
        Math.max(0,  - other.missCount),
        Math.max(0,  - other.loadSuccessCount),
        Math.max(0,  - other.loadExceptionCount),
        Math.max(0,  - other.totalLoadTime),
        Math.max(0,  - other.evictionCount));
  }

  
Returns a new CacheStats representing the sum of this CacheStats and other.

Since:
11.0
  public CacheStats plus(CacheStats other) {
    return new CacheStats(
         + other.hitCount,
         + other.missCount,
         + other.loadSuccessCount,
         + other.loadExceptionCount,
         + other.totalLoadTime,
         + other.evictionCount);
  }
  public int hashCode() {
        );
  }
  public boolean equals(@Nullable Object object) {
    if (object instanceof CacheStats) {
      CacheStats other = (CacheStatsobject;
      return  == other.hitCount
          &&  == other.missCount
          &&  == other.loadSuccessCount
          &&  == other.loadExceptionCount
          &&  == other.totalLoadTime
          &&  == other.evictionCount;
    }
    return false;
  }
  public String toString() {
    return Objects.toStringHelper(this)
        .add("hitCount")
        .add("missCount")
        .add("loadSuccessCount")
        .add("loadExceptionCount")
        .add("totalLoadTime")
        .add("evictionCount")
        .toString();
  }
New to GrepCode? Check out our FAQ X