1 23 24 package org.apache.slide.util; 25 26 31 public abstract class AbstractObjectCache implements ObjectCache { 32 33 34 36 37 40 protected static final int DEFAULT_SIZE = 1000; 41 42 43 46 protected static final double DEFAULT_HIT_RATIO = 0.8; 47 48 49 51 52 58 public AbstractObjectCache() { 59 this(DEFAULT_SIZE, Integer.MAX_VALUE, DEFAULT_HIT_RATIO); 60 } 61 62 63 70 public AbstractObjectCache(int initialSize, int maxSize, 71 double desiredHitRatio) { 72 this.initialSize = initialSize; 73 this.maxSize = maxSize; 74 this.desiredHitRatio = desiredHitRatio; 75 } 76 77 78 80 81 84 protected int initialSize; 85 86 87 90 protected double desiredHitRatio; 91 92 93 96 protected int maxSize; 97 98 99 101 104 protected double cacheRequests; 105 106 107 110 protected double cacheHits; 111 112 113 119 protected double currentHitRatio; 120 121 122 124 125 132 public abstract Object get(Object key); 133 134 135 141 public abstract void put(Object key, Object value); 142 143 144 150 public abstract void remove(Object key); 151 152 153 156 public abstract void clear(); 157 158 159 161 162 166 protected abstract void removeSomeObjects(); 167 168 169 174 protected abstract int getSize(); 175 176 177 180 protected abstract void resize(); 181 182 183 186 protected void shouldResize() { 187 if (cacheRequests > getSize()) { 188 currentHitRatio = cacheHits / cacheRequests; 189 if (currentHitRatio > desiredHitRatio) { 190 resize(); 191 } 192 } 193 } 194 195 196 } 197 | Popular Tags |