01: /*
02: Copyright 2004 Philip Jacob <phil@whirlycott.com>
03: Seth Fitzsimmons <seth@note.amherst.edu>
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: 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;
21: import java.util.Collections;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Map.Entry;
25: import java.util.concurrent.ConcurrentHashMap;
26:
27: import org.apache.commons.logging.Log;
28: import org.apache.commons.logging.LogFactory;
29:
30: import com.whirlycott.cache.CacheMaintenancePolicy;
31: import com.whirlycott.cache.ManagedCache;
32: import com.whirlycott.cache.Messages;
33:
34: /**
35: * This policy removes cached items, biased towards least recently used (LRU) Items.
36: *
37: * @author Seth Fitzsimmons
38: */
39: public class LRUMaintenancePolicy<K, V> implements
40: CacheMaintenancePolicy<K, V> {
41:
42: private static final Log log = LogFactory
43: .getLog(LRUMaintenancePolicy.class);
44:
45: public void performMaintenance(ManagedCache<K, V> managedCache,
46: int maxSize) {
47: if (log.isDebugEnabled()) {
48: log
49: .debug(Messages
50: .getString("LRUMaintenancePolicy.performing_lru_maintenance"));
51:
52: log.debug(Messages.getCompoundString(
53: "CacheMaintenancePolicy.report_items", maxSize,
54: managedCache.size()));
55: }
56:
57: // Sort the entries in the cache.
58: final List<Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>(
59: new ConcurrentHashMap<K, V>(managedCache).entrySet());
60: int currentSize = managedCache.size();
61:
62: if (maxSize < currentSize) {
63: if (log.isDebugEnabled()) {
64: log
65: .debug(Messages
66: .getCompoundString(
67: "CacheMaintenancePolicy.clearing_approximately",
68: currentSize - maxSize)); //$NON-NLS-1$
69: }
70:
71: Collections.sort(entries, new UsedComparator());
72:
73: final List<Map.Entry<K, V>> removeThese = entries.subList(
74: 0, currentSize - maxSize);
75:
76: for (Entry<K, V> entry : removeThese) {
77: if (entry != null) {
78: // final Item evictee = (Item) entry.getValue();
79: // log.trace("Removing: " + entry.getKey() + " (" +
80: // evictee.added + ", " + evictee.used + ", " + evictee.count +
81: // ")");
82: managedCache.remove(entry.getKey());
83: }
84: }
85: if (log.isDebugEnabled()) {
86: log
87: .debug(Messages
88: .getString("LRUMaintenancePolicy.new_size") + managedCache.size()); //$NON-NLS-1$
89: }
90: }
91: }
92: }
|