KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.sape.carbon.core.component.ComponentConfiguration;
21 import org.sape.carbon.services.cache.CacheLoadException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * Extends AbsractMRUCache to provide the get method
28  *
29  * Copyright 2002 Sapient
30  * @since carbon 1.0
31  * @author Doug Voet, April 2002
32  * @version $Revision: 1.9 $($Author: dvoet $ / $Date: 2003/05/05 21:21:07 $)
33  */

34 public class DefaultMRUCacheImpl extends AbstractMRUCache {
35     /** Holds the loader used to load this cache. */
36     protected MRUCacheDataLoader dataLoader;
37
38     /** The handle to Apache-commons logger */
39     private Log log = LogFactory.getLog(this.getClass());
40
41     /**
42      * Configures the component with a <code>MRUCacheConfiguration</code>.
43      *
44      * @param configuration <code>MRUCacheConfiguration</code> used to
45      * configure the component.
46      */

47     public void configure(ComponentConfiguration configuration) {
48         super.configure(configuration);
49         this.dataLoader =
50             ((MRUCacheConfiguration) configuration).getDataLoader();
51     }
52
53     /**
54      * <p>For a given key value, return the object associated with the key
55      * value from the cache. If the value is not found in the cache OR if the
56      * value in the cache has expired, the cache will attempt to load it from
57      * the DataLoader. If the DataLoader returns a null value, a null value
58      * will be stored in the cache and returned to the caller.</p>
59      *
60      * <p>Unlike other methods on the MRUCache class, this method is not
61      * synchronized at the method level. It is, however, synchronized within the
62      * method. The purpose of this is to allow the cache to continue servicing
63      * requests while it is loading data from the DataLoader. All other
64      * activities in this method are synchronized, but the load of data from the
65      * MRUCacheDataLoader is unsycnchronized, and will not stop other threads
66      * from reading from the cache, or loading other datums.</p>
67      *
68      * @param key the key for the desired cache entry
69      * @return <code>Object</code> the cached object
70      */

71     public Object JavaDoc get(Object JavaDoc key) {
72         Object JavaDoc value = getObject(key);
73
74         // If it's not there...get it from the dataloader and put it
75
// into the cache. Note that this step is not synchronized!
76
if (value == null) {
77             this.cacheMisses++;
78
79             try {
80                 value = this.dataLoader.loadDatum(key);
81
82                 // Seeing as how we just had a cache miss, check to see if we
83
// are loggin detailed information. If we are, then log
84
// information about the cache hit rate statistics.
85
if (log.isDebugEnabled()) {
86                     log.debug("missed on cache: " + this.toString());
87                 }
88
89                 synchronized (this) {
90                     // Now that we have loaded from the external source, do the
91
// double-check, and if nobody else put it into the cache in
92
// the mean time, drop it in!
93
if (this.map.get(key) == null) {
94                         put(key, value);
95                     }
96                 }
97             } catch (CacheLoadException cle) {
98                 if (log.isWarnEnabled()) {
99                     log.warn("Caught CacheLoadException loading datum "
100                         + "with key ["
101                         + key
102                         + "], returning null: "
103                         + cle);
104                 }
105             }
106         }
107
108         // Finally return the value that we found either from the cache or
109
// the DataLoader
110
return value;
111     }
112 }
Popular Tags