1 package org.apache.ojb.broker.cache; 2 3 17 18 import java.lang.ref.ReferenceQueue ; 19 import java.lang.ref.SoftReference ; 20 import java.util.HashMap ; 21 import java.util.Properties ; 22 23 import org.apache.commons.collections.LRUMap; 24 import org.apache.ojb.broker.Identity; 25 import org.apache.ojb.broker.PersistenceBroker; 26 import org.apache.ojb.broker.util.configuration.Configurable; 27 import org.apache.ojb.broker.util.configuration.Configuration; 28 import org.apache.ojb.broker.util.configuration.ConfigurationException; 29 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator; 30 31 37 public class ObjectCacheSoftImpl implements ObjectCache, Configurable 38 { 39 42 private static SoftHashMap cache = null; 43 44 47 private static int size = 10000; 48 49 55 public ObjectCacheSoftImpl(PersistenceBroker broker, Properties properties) 56 { 57 if (cache == null) 58 { 59 OjbConfigurator.getInstance().configure(this); 60 cache = new SoftHashMap(size); 61 } 62 } 63 64 67 public void configure(Configuration configuration) throws ConfigurationException 68 { 69 size = configuration.getInteger("ObjectCacheSoftImpl", size); 70 } 71 72 75 public void cache(Identity oid, Object obj) 76 { 77 synchronized(cache) 78 { 79 cache.put(oid, obj); 80 } 81 } 82 83 public boolean cacheIfNew(Identity oid, Object obj) 84 { 85 synchronized(cache) 86 { 87 if(cache.get(oid) == null) 88 { 89 cache.put(oid, obj); 90 return true; 91 } 92 return false; 93 } 94 } 95 96 99 public Object lookup(Identity oid) 100 { 101 return cache.get(oid); 102 } 103 104 107 public void remove(Identity oid) 108 { 109 synchronized(cache) 110 { 111 cache.remove(oid); 112 } 113 } 114 115 118 public void clear() 119 { 120 cache.clear(); 121 } 122 123 126 public static final class SoftHashMap 127 { 128 131 private HashMap hash; 132 135 private LRUMap hardCacheMap; 136 139 private ReferenceQueue queue; 140 141 146 public SoftHashMap(final int hardSize) 147 { 148 hash = new HashMap (); 149 hardCacheMap = new LRUMap(hardSize); 150 queue = new ReferenceQueue (); 151 } 152 153 160 public Object put(Object key, Object value) 161 { 162 if (key == null || value == null) 164 { 165 return null; 166 } 167 processQueue(); hardCacheMap.put(key, value); 169 return hash.put(key, new SoftValue(value, key, queue)); 170 } 171 172 178 public Object get(Object key) 179 { 180 if (key == null) 182 { 183 return null; 184 } 185 Object result = null; 186 SoftReference softRef = (SoftReference ) hash.get(key); 188 if (softRef != null) 189 { 190 result = softRef.get(); 191 if (result == null) 192 { 193 hash.remove(key); 196 } 197 else 198 { 199 if (!hardCacheMap.containsKey(key)) 200 { 201 hardCacheMap.put(key, result); 202 } 203 else 204 { 205 hardCacheMap.get(key); 206 } 207 } 208 } 209 return result; 210 } 211 212 218 public Object remove(Object key) 219 { 220 processQueue(); Object retval = null; 222 Object value = hash.remove(key); 223 if (value != null) 224 { 225 if (value instanceof SoftValue) 226 { 227 retval = ((SoftValue) value).get(); 228 } 229 } 230 return retval; 231 } 232 233 236 public void clear() 237 { 238 processQueue(); 239 hash.clear(); 240 hardCacheMap.clear(); 241 } 242 243 247 private class SoftValue extends SoftReference 248 { 249 252 private final Object key; 254 261 private SoftValue(final Object k, final Object key, final ReferenceQueue q) 262 { 263 super(k, q); 264 this.key = key; 265 } 266 } 267 268 271 private void processQueue() 272 { 273 SoftValue sv; 274 while ((sv = (SoftValue) queue.poll()) != null) 275 { 276 hash.remove(sv.key); } 278 } 279 280 } 281 } 282 | Popular Tags |