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 insertCount; 025 026 protected long updateCount; 027 028 protected long removeCount; 029 030 protected long clearCount; 031 032 protected long evictionRunCount; 033 034 protected long evictionRunMicros; 035 036 protected long evictByIdle; 037 038 protected long evictByTTL; 039 040 protected long evictByLRU; 041 042 @Override 043 public String toString() { 044 //noinspection StringBufferReplaceableByString 045 StringBuilder sb = new StringBuilder(80); 046 sb.append(cacheName); 047 sb.append(" maxSize:").append(maxSize); 048 sb.append(" size:").append(size); 049 sb.append(" hitRatio:").append(getHitRatio()); 050 sb.append(" hit:").append(hitCount); 051 sb.append(" miss:").append(missCount); 052 sb.append(" insert:").append(insertCount); 053 sb.append(" update:").append(updateCount); 054 sb.append(" remove:").append(removeCount); 055 sb.append(" clear:").append(clearCount); 056 sb.append(" evictByIdle:").append(evictByIdle); 057 sb.append(" evictByTTL:").append(evictByTTL); 058 sb.append(" evictByLRU:").append(evictByLRU); 059 sb.append(" evictionRunCount:").append(evictionRunCount); 060 sb.append(" evictionRunMicros:").append(evictionRunMicros); 061 return sb.toString(); 062 } 063 064 /** 065 * Returns an int from 0 to 100 (percentage) for the hit ratio. 066 * <p> 067 * A hit ratio of 100 means every get request against the cache hits an entry. 068 * </p> 069 */ 070 public int getHitRatio() { 071 long totalCount = hitCount + missCount; 072 if (totalCount == 0) { 073 return 0; 074 } else { 075 return (int) (hitCount * 100 / totalCount); 076 } 077 } 078 079 /** 080 * Return the name of the cache. 081 */ 082 public String getCacheName() { 083 return cacheName; 084 } 085 086 /** 087 * Set the name of the cache. 088 */ 089 public void setCacheName(String cacheName) { 090 this.cacheName = cacheName; 091 } 092 093 /** 094 * Return the hit count. The number of successful gets. 095 */ 096 public long getHitCount() { 097 return hitCount; 098 } 099 100 /** 101 * Set the hit count. 102 */ 103 public void setHitCount(long hitCount) { 104 this.hitCount = hitCount; 105 } 106 107 /** 108 * Return the miss count. The number of gets that returned null. 109 */ 110 public long getMissCount() { 111 return missCount; 112 } 113 114 /** 115 * Set the miss count. 116 */ 117 public void setMissCount(long missCount) { 118 this.missCount = missCount; 119 } 120 121 /** 122 * Return the size of the cache. 123 */ 124 public int getSize() { 125 return size; 126 } 127 128 /** 129 * Set the size of the cache. 130 */ 131 public void setSize(int size) { 132 this.size = size; 133 } 134 135 /** 136 * Return the maximum size of the cache. 137 * <p> 138 * Can be used in conjunction with the size to determine if the cache use is 139 * being potentially limited by its maximum size. 140 * </p> 141 */ 142 public int getMaxSize() { 143 return maxSize; 144 } 145 146 /** 147 * Set the maximum size of the cache. 148 */ 149 public void setMaxSize(int maxSize) { 150 this.maxSize = maxSize; 151 } 152 153 /** 154 * Set the put insert count. 155 */ 156 public void setInsertCount(long insertCount) { 157 this.insertCount = insertCount; 158 } 159 160 /** 161 * Return the put insert count. 162 */ 163 public long getInsertCount() { 164 return insertCount; 165 } 166 167 /** 168 * Set the put update count. 169 */ 170 public void setUpdateCount(long updateCount) { 171 this.updateCount = updateCount; 172 } 173 174 /** 175 * Return the put update count. 176 */ 177 public long getUpdateCount() { 178 return updateCount; 179 } 180 181 /** 182 * Set the remove count. 183 */ 184 public void setRemoveCount(long removeCount) { 185 this.removeCount = removeCount; 186 } 187 188 /** 189 * Return the remove count. 190 */ 191 public long getRemoveCount() { 192 return removeCount; 193 } 194 195 /** 196 * Set the clear count. 197 */ 198 public void setClearCount(long clearCount) { 199 this.clearCount = clearCount; 200 } 201 202 /** 203 * Return the clear count. 204 */ 205 public long getClearCount() { 206 return clearCount; 207 } 208 209 /** 210 * Set the eviction run count. 211 */ 212 public void setEvictionRunCount(long evictCount) { 213 this.evictionRunCount = evictCount; 214 } 215 216 /** 217 * Return the eviction run count. 218 */ 219 public long getEvictionRunCount() { 220 return evictionRunCount; 221 } 222 223 /** 224 * Set the eviction run time in micros. 225 */ 226 public void setEvictionRunMicros(long evictionRunMicros) { 227 this.evictionRunMicros = evictionRunMicros; 228 } 229 230 /** 231 * Return the eviction run time in micros. 232 */ 233 public long getEvictionRunMicros() { 234 return evictionRunMicros; 235 } 236 237 /** 238 * Set the count of entries evicted due to idle time. 239 */ 240 public void setEvictByIdle(long evictByIdle) { 241 this.evictByIdle = evictByIdle; 242 } 243 244 /** 245 * Return the count of entries evicted due to idle time. 246 */ 247 public long getEvictByIdle() { 248 return evictByIdle; 249 } 250 251 /** 252 * Set the count of entries evicted due to time to live. 253 */ 254 public void setEvictByTTL(long evictByTTL) { 255 this.evictByTTL = evictByTTL; 256 } 257 258 /** 259 * Return the count of entries evicted due to time to live. 260 */ 261 public long getEvictByTTL() { 262 return evictByTTL; 263 } 264 265 /** 266 * Set the count of entries evicted due to time least recently used. 267 */ 268 public void setEvictByLRU(long evictByLRU) { 269 this.evictByLRU = evictByLRU; 270 } 271 272 /** 273 * Return the count of entries evicted due to time least recently used. 274 */ 275 public long getEvictByLRU() { 276 return evictByLRU; 277 } 278}