KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > policy > LRUMaintenancePolicy


1 /*
2 Copyright 2004 Philip Jacob <phil@whirlycott.com>
3                     Seth Fitzsimmons <seth@note.amherst.edu>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */

17
18 package com.whirlycott.cache.policy;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Map.Entry;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import com.whirlycott.cache.CacheConfiguration;
31 import com.whirlycott.cache.CacheMaintenancePolicy;
32 import com.whirlycott.cache.ManagedCache;
33 import com.whirlycott.cache.Messages;
34
35 /**
36  * This policy removes cached items, biased towards least recently used (LRU)
37  * Items.
38  *
39  * @author Seth Fitzsimmons
40  */

41 public class LRUMaintenancePolicy implements CacheMaintenancePolicy {
42
43     private static final Log log = LogFactory.getLog( LRUMaintenancePolicy.class );
44
45     protected ManagedCache managedCache = null;
46
47     protected int maxSize;
48
49     public void performMaintenance() {
50         log.debug( Messages.getString("LRUMaintenancePolicy.performing_lru_maintenance") ); //$NON-NLS-1$
51

52         final Object JavaDoc[] args = {
53                 new Integer JavaDoc(maxSize),
54                 new Integer JavaDoc(managedCache.size())
55         };
56         log.debug( Messages.getCompoundString("CacheMaintenancePolicy.report_items", args) ); //$NON-NLS-1$
57

58         // Sort the entries in the cache.
59
final List JavaDoc entries = new ArrayList JavaDoc( managedCache.entrySet() );
60         int currentSize = managedCache.size();
61         if ( maxSize < currentSize ) {
62             final Object JavaDoc[] args1 = {
63                     new Integer JavaDoc(currentSize - maxSize)
64             };
65             log.debug( Messages.getCompoundString("CacheMaintenancePolicy.clearing_approximately", args1) ); //$NON-NLS-1$
66
Collections.sort( entries, new UsedComparator() );
67             final List JavaDoc removeThese = entries.subList( 0, currentSize - maxSize );
68             for (final Iterator JavaDoc i = removeThese.iterator(); i.hasNext();) {
69                 final Map.Entry JavaDoc entry = (Entry) i.next();
70                 if (entry != null) {
71                     // final Item evictee = (Item) entry.getValue();
72
// log.trace("Removing: " + entry.getKey() + " (" +
73
// evictee.added + ", " + evictee.used + ", " + evictee.count +
74
// ")");
75
managedCache.remove( entry.getKey() );
76                 }
77             }
78             log.debug( Messages.getString("LRUMaintenancePolicy.new_size") + managedCache.size() ); //$NON-NLS-1$
79
}
80
81     }
82
83     public void setCache(ManagedCache _cache) {
84         managedCache = _cache;
85     }
86
87     public void setConfiguration(CacheConfiguration _configuration) {
88         maxSize = _configuration.getMaxSize();
89     }
90 }
Popular Tags