1 17 18 package org.sape.carbon.services.cache.mru; 19 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.sape.carbon.core.component.ComponentConfiguration; 28 import org.sape.carbon.core.component.lifecycle.Configurable; 29 import org.sape.carbon.core.component.lifecycle.Initializable; 30 import org.sape.carbon.services.cache.CacheLoadException; 31 import org.sape.carbon.services.cache.MultiGetCache; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 44 public class MultiGetMRUCache extends AbstractMRUCache 45 implements MultiGetCache, MRUCache, Configurable, Initializable { 46 47 48 private Log log = LogFactory.getLog(this.getClass()); 49 50 51 protected MultiGetMRUCacheDataLoader dataLoader; 52 53 56 public void configure(ComponentConfiguration configuration) { 57 super.configure(configuration); 58 59 this.dataLoader = 60 ((MultiGetMRUCacheConfiguration) configuration).getDataLoader(); 61 } 62 63 66 public Map getMultiple(Set keys) { 67 Map found = new HashMap (keys.size()); 69 70 Set notFound = new HashSet (keys.size()); 72 73 Iterator keyIterator = keys.iterator(); 74 while (keyIterator.hasNext()) { 75 Object key = keyIterator.next(); 76 Object value = getObject(key); if (value == null) { 78 notFound.add(key); 80 this.cacheMisses++; 81 } else { 82 found.put(key, value); 84 this.cacheHits++; 85 } 86 } 87 88 if (!notFound.isEmpty()) { 91 92 93 if (log.isDebugEnabled()) { 97 log.debug("missed on cache: " + this.toString()); 98 } 99 100 try { 101 Map added = this.dataLoader.loadData(notFound); 102 found.putAll(added); 103 putAll(added); 104 } catch (CacheLoadException cle) { 105 if (log.isWarnEnabled()) { 106 log.warn("Caught CacheLoadException loading data " 107 + "with keys [" 108 + notFound 109 + "], returning only data already within the cache: " 110 + cle); 111 } 112 } 113 } 114 return found; 115 } 116 117 120 public Object get(Object key) { 121 return getMultiple(Collections.singleton(key)); 122 } 123 } | Popular Tags |