KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > cache > LRUCacheImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.util.cache;
20
21 import java.util.Collections JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.LinkedHashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30 /**
31  * A simple LRU Cache.
32  */

33 public class LRUCacheImpl implements Cache {
34     
35     private static Log log = LogFactory.getLog(LRUCacheImpl.class);
36     
37     private String JavaDoc id = null;
38     private Map JavaDoc cache = null;
39     
40     // for metrics
41
protected double hits = 0;
42     protected double misses = 0;
43     protected double puts = 0;
44     protected double removes = 0;
45     protected Date JavaDoc startTime = new Date JavaDoc();
46     
47     
48     protected LRUCacheImpl(String JavaDoc id) {
49         
50         this.id = id;
51         this.cache = Collections.synchronizedMap(new LRULinkedHashMap(100));
52     }
53     
54     
55     protected LRUCacheImpl(String JavaDoc id, int maxsize) {
56         
57         this.id = id;
58         this.cache = Collections.synchronizedMap(new LRULinkedHashMap(maxsize));
59     }
60     
61     
62     public String JavaDoc getId() {
63         return this.id;
64     }
65     
66     
67     /**
68      * Store an entry in the cache.
69      */

70     public synchronized void put(String JavaDoc key, Object JavaDoc value) {
71         
72         this.cache.put(key, value);
73         puts++;
74     }
75     
76     
77     /**
78      * Retrieve an entry from the cache.
79      */

80     public synchronized Object JavaDoc get(String JavaDoc key) {
81         
82         Object JavaDoc obj = this.cache.get(key);
83         
84         // for metrics
85
if(obj == null) {
86             misses++;
87         } else {
88             hits++;
89         }
90         
91         return obj;
92     }
93     
94     
95     public synchronized void remove(String JavaDoc key) {
96         
97         this.cache.remove(key);
98         removes++;
99     }
100     
101     
102     public synchronized void clear() {
103         
104         this.cache.clear();
105         
106         // clear metrics
107
hits = 0;
108         misses = 0;
109         puts = 0;
110         removes = 0;
111         startTime = new Date JavaDoc();
112     }
113     
114     
115     public Map JavaDoc getStats() {
116         
117         Map JavaDoc stats = new HashMap JavaDoc();
118         stats.put("startTime", this.startTime);
119         stats.put("hits", new Double JavaDoc(this.hits));
120         stats.put("misses", new Double JavaDoc(this.misses));
121         stats.put("puts", new Double JavaDoc(this.puts));
122         stats.put("removes", new Double JavaDoc(this.removes));
123         
124         // calculate efficiency
125
if((misses - removes) > 0) {
126             double efficiency = hits / (misses + hits);
127             stats.put("efficiency", new Double JavaDoc(efficiency * 100));
128         }
129         
130         return stats;
131     }
132     
133     
134     // David Flanaghan: http://www.davidflanagan.com/blog/000014.html
135
private static class LRULinkedHashMap extends LinkedHashMap JavaDoc {
136         protected int maxsize;
137         
138         public LRULinkedHashMap(int maxsize) {
139             super(maxsize * 4 / 3 + 1, 0.75f, true);
140             this.maxsize = maxsize;
141         }
142         
143         protected boolean removeEldestEntry(Map.Entry JavaDoc eldest) {
144             return this.size() > this.maxsize;
145         }
146     }
147     
148 }
149
Popular Tags