001package io.ebean.cache;
002
003/**
004 * The statistics collected per cache.
005 * <p>
006 * These can be monitored to review the effectiveness of a particular cache.
007 * </p>
008 * <p>
009 * Depending on the cache implementation not all the statistics may be collected.
010 * </p>
011 */
012public class ServerCacheStatistics {
013
014  protected String cacheName;
015
016  protected int maxSize;
017
018  protected int size;
019
020  protected long hitCount;
021
022  protected long missCount;
023
024  protected long putCount;
025
026  protected long removeCount;
027
028  protected long clearCount;
029
030  protected long evictCount;
031
032  @Override
033  public String toString() {
034    //noinspection StringBufferReplaceableByString
035    StringBuilder sb = new StringBuilder(80);
036    sb.append(cacheName);
037    sb.append(" maxSize:").append(maxSize);
038    sb.append(" size:").append(size);
039    sb.append(" hitRatio:").append(getHitRatio());
040    sb.append(" hit:").append(hitCount);
041    sb.append(" miss:").append(missCount);
042    sb.append(" put:").append(putCount);
043    sb.append(" remove:").append(removeCount);
044    sb.append(" clear:").append(clearCount);
045    sb.append(" evict:").append(evictCount);
046    return sb.toString();
047  }
048
049  /**
050   * Returns an int from 0 to 100 (percentage) for the hit ratio.
051   * <p>
052   * A hit ratio of 100 means every get request against the cache hits an entry.
053   * </p>
054   */
055  public int getHitRatio() {
056    long totalCount = hitCount + missCount;
057    if (totalCount == 0) {
058      return 0;
059    } else {
060      return (int) (hitCount * 100 / totalCount);
061    }
062  }
063
064  /**
065   * Return the name of the cache.
066   */
067  public String getCacheName() {
068    return cacheName;
069  }
070
071  /**
072   * Set the name of the cache.
073   */
074  public void setCacheName(String cacheName) {
075    this.cacheName = cacheName;
076  }
077
078  /**
079   * Return the hit count. The number of successful gets.
080   */
081  public long getHitCount() {
082    return hitCount;
083  }
084
085  /**
086   * Set the hit count.
087   */
088  public void setHitCount(long hitCount) {
089    this.hitCount = hitCount;
090  }
091
092  /**
093   * Return the miss count. The number of gets that returned null.
094   */
095  public long getMissCount() {
096    return missCount;
097  }
098
099  /**
100   * Set the miss count.
101   */
102  public void setMissCount(long missCount) {
103    this.missCount = missCount;
104  }
105
106  /**
107   * Return the size of the cache.
108   */
109  public int getSize() {
110    return size;
111  }
112
113  /**
114   * Set the size of the cache.
115   */
116  public void setSize(int size) {
117    this.size = size;
118  }
119
120  /**
121   * Return the maximum size of the cache.
122   * <p>
123   * Can be used in conjunction with the size to determine if the cache use is
124   * being potentially limited by its maximum size.
125   * </p>
126   */
127  public int getMaxSize() {
128    return maxSize;
129  }
130
131  /**
132   * Set the maximum size of the cache.
133   */
134  public void setMaxSize(int maxSize) {
135    this.maxSize = maxSize;
136  }
137
138  /**
139   * Set the put insert count.
140   */
141  public void setPutCount(long putCount) {
142    this.putCount = putCount;
143  }
144
145  /**
146   * Return the put insert count.
147   */
148  public long getPutCount() {
149    return putCount;
150  }
151
152  /**
153   * Set the remove count.
154   */
155  public void setRemoveCount(long removeCount) {
156    this.removeCount = removeCount;
157  }
158
159  /**
160   * Return the remove count.
161   */
162  public long getRemoveCount() {
163    return removeCount;
164  }
165
166  /**
167   * Set the clear count.
168   */
169  public void setClearCount(long clearCount) {
170    this.clearCount = clearCount;
171  }
172
173  /**
174   * Return the clear count.
175   */
176  public long getClearCount() {
177    return clearCount;
178  }
179
180  /**
181   * Set the count of entries evicted due to idle time.
182   */
183  public void setEvictCount(long evictCount) {
184    this.evictCount = evictCount;
185  }
186
187  /**
188   * Return the count of entries evicted due to idle time.
189   */
190  public long getEvictCount() {
191    return evictCount;
192  }
193
194}