KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > cache > mru > MultiGetMRUCache


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.cache.mru;
19
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
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 /**
37  * This class extends the AbstractMRUCache to add multi-get functionality.
38  *
39  * Copyright 2002 Sapient
40  * @since carbon 1.0
41  * @author Doug Voet, April 2002
42  * @version $Revision: 1.11 $($Author: dvoet $ / $Date: 2003/05/05 21:21:07 $)
43  */

44 public class MultiGetMRUCache extends AbstractMRUCache
45     implements MultiGetCache, MRUCache, Configurable, Initializable {
46
47     /** The handle to Apache-commons logger */
48     private Log log = LogFactory.getLog(this.getClass());
49
50     /** Holds the dataloader for this cache. */
51     protected MultiGetMRUCacheDataLoader dataLoader;
52
53     /**
54      * @see org.sape.carbon.core.component.lifecycle.Configurable#configure(ComponentConfiguration)
55      */

56     public void configure(ComponentConfiguration configuration) {
57         super.configure(configuration);
58
59         this.dataLoader =
60             ((MultiGetMRUCacheConfiguration) configuration).getDataLoader();
61     }
62
63     /**
64      * @see org.sape.carbon.services.cache.MultiGetCache#getMultiple(Set)
65      */

66     public Map JavaDoc getMultiple(Set JavaDoc keys) {
67         // The found keys as we retrieve them from the cache and then the loader
68
Map JavaDoc found = new HashMap JavaDoc(keys.size());
69
70         // The set of keys that could not be immediately found in the cache
71
Set JavaDoc notFound = new HashSet JavaDoc(keys.size());
72
73         Iterator JavaDoc keyIterator = keys.iterator();
74         while (keyIterator.hasNext()) {
75             Object JavaDoc key = keyIterator.next();
76             Object JavaDoc value = getObject(key); // doesn't call dataloader
77
if (value == null) {
78                 // Not currently cached value - Place in map to be retrieved
79
notFound.add(key);
80                 this.cacheMisses++;
81             } else {
82                 // Found in current cache
83
found.put(key, value);
84                 this.cacheHits++;
85             }
86         }
87
88         // If there are keys that have not been found then
89
// look them up all at once
90
if (!notFound.isEmpty()) {
91
92
93             // Seeing as how we just had a cache miss, check to see if we
94
// are loggin detailed information. If we are, then log information
95
// about the cache hit rate statistics.
96
if (log.isDebugEnabled()) {
97                 log.debug("missed on cache: " + this.toString());
98             }
99
100             try {
101                 Map JavaDoc 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     /**
118      * @see java.util.Map#get(Object)
119      */

120     public Object JavaDoc get(Object JavaDoc key) {
121         return getMultiple(Collections.singleton(key));
122     }
123 }
Popular Tags