1 23 24 package org.apache.slide.util; 25 26 33 public class HashMapObjectCache extends AbstractObjectCache { 34 35 36 38 39 45 public HashMapObjectCache() { 46 super(); 47 this.cache = new HashMap(this.initialSize); 48 } 49 50 51 58 public HashMapObjectCache(int initialSize, int maxSize, 59 double desiredHitRatio) { 60 super(initialSize, maxSize, desiredHitRatio); 61 this.cache = new HashMap(this.initialSize / 3); 62 } 63 64 65 67 68 71 protected HashMap cache; 72 73 74 76 77 84 public Object get(Object key) { 85 synchronized (cache) { 86 Object result = cache.get(key); 87 this.cacheRequests += 1; 88 89 if (result != null) { 90 this.cacheHits += 1; 91 } else { 92 shouldResize(); 93 } 94 return result; 95 } 96 } 97 98 99 105 public void put(Object key, Object value) { 106 synchronized (cache) { 107 cache.put(key, value); 108 } 109 } 110 111 112 118 public void remove(Object key) { 119 synchronized (cache) { 120 cache.remove(key); 121 } 122 } 123 124 125 128 public void clear() { 129 synchronized (cache) { 130 cache.clear(); 131 } 132 } 133 134 135 137 138 142 protected void removeSomeObjects() { 143 clear(); 144 } 145 146 147 152 protected int getSize() { 153 return cache.size(); 154 } 155 156 157 160 protected void resize() { 161 synchronized (cache) { 162 if (getSize() < maxSize) { 163 int size = getSize(); 164 cache.clear(); 165 cache = new HashMap(size / 3 * 2); 166 } 167 } 168 } 169 170 171 } 172 | Popular Tags |