KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > LRUCache2


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 package org.apache.roller.util;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 /**
26  * LRU cache with per-entry timeout logic.
27  *
28  * @author Dave Johnson
29  */

30 public class LRUCache2
31 {
32     private long timeout;
33     private Map JavaDoc cache = null;
34     private Environment environment = null;
35
36     /**
37      * Create cache.
38      *
39      * @param maxsize
40      * Maximum number of entries in cache.
41      * @param timeout
42      * Entry timeout in milli-seconds.
43      */

44     public LRUCache2(int maxsize, long timeout)
45     {
46         this.environment = new DefaultEnvironment();
47         this.timeout = timeout;
48         this.cache = new LRULinkedHashMap(maxsize);
49     }
50
51     /**
52      * Create cache that uses custom environment.
53      *
54      * @param maxsize
55      * Maximum number of entries in cache.
56      * @param timeout
57      * Entry timeout in milli-seconds.
58      */

59     public LRUCache2(Environment environment, int maxsize, long timeout)
60     {
61         this.environment = environment;
62         this.timeout = timeout;
63         this.cache = new LRULinkedHashMap(maxsize);
64     }
65
66     public synchronized void put(Object JavaDoc key, Object JavaDoc value)
67     {
68         CacheEntry entry = new CacheEntry(value, environment
69                         .getCurrentTimeInMillis());
70         cache.put(key, entry);
71     }
72
73     public Object JavaDoc get(Object JavaDoc key)
74     {
75         Object JavaDoc value = null;
76         CacheEntry entry = null;
77         synchronized(this)
78         {
79             entry = (CacheEntry) cache.get(key);
80         }
81         if (entry != null)
82         {
83             if (environment.getCurrentTimeInMillis() - entry.getTimeCached() < timeout)
84             {
85                 value = entry.getValue();
86             }
87             else
88             {
89                 cache.remove(entry);
90             }
91         }
92         return value;
93     }
94
95     public synchronized void purge()
96     {
97         cache.clear();
98     }
99
100     public synchronized void purge(String JavaDoc[] patterns)
101     {
102         List JavaDoc purgeList = new ArrayList JavaDoc();
103         Iterator JavaDoc keys = cache.keySet().iterator();
104         while (keys.hasNext())
105         {
106             String JavaDoc key = (String JavaDoc) keys.next();
107             for (int i = 0; i < patterns.length; i++)
108             {
109                 if (key.indexOf(patterns[i]) != -1)
110                 {
111                     purgeList.add(key);
112                     break;
113                 }
114             }
115         }
116         Iterator JavaDoc purgeIter = purgeList.iterator();
117         while (purgeIter.hasNext())
118         {
119             String JavaDoc key = (String JavaDoc) purgeIter.next();
120             cache.remove(key);
121         }
122     }
123
124     public int size()
125     {
126         return cache.size();
127     }
128     public interface Environment
129     {
130         public long getCurrentTimeInMillis();
131     }
132     public static class DefaultEnvironment implements Environment
133     {
134         public long getCurrentTimeInMillis()
135         {
136             return System.currentTimeMillis();
137         }
138     }
139     private static class CacheEntry
140     {
141         private Object JavaDoc value;
142         private long timeCached = -1;
143
144         public CacheEntry(Object JavaDoc value, long timeCached)
145         {
146             this.timeCached = timeCached;
147             this.value = value;
148         }
149
150         public long getTimeCached()
151         {
152             return timeCached;
153         }
154
155         public Object JavaDoc getValue()
156         {
157             return value;
158         }
159     }
160     
161     // David Flanaghan: http://www.davidflanagan.com/blog/000014.html
162
private static class LRULinkedHashMap extends LinkedHashMap JavaDoc
163     {
164         protected int maxsize;
165
166         public LRULinkedHashMap(int maxsize)
167         {
168             super(maxsize * 4 / 3 + 1, 0.75f, true);
169             this.maxsize = maxsize;
170         }
171
172         protected boolean removeEldestEntry(Map.Entry JavaDoc eldest)
173         {
174             return this.size() > this.maxsize;
175         }
176     }
177 }
178
Popular Tags