1 5 6 7 12 13 package org.joseki.util.cache; 14 15 import java.util.* ; 16 import org.apache.commons.logging.*; 17 18 public class CachePolicyLRU implements CachePolicy 19 { 20 static Log log = LogFactory.getLog(CachePolicyLRU.class.getName()) ; 21 22 26 28 34 LinkedList list = new LinkedList() ; 36 37 public Object [] expel(int count) 38 { 39 if ( log.isTraceEnabled() ) 40 log.trace("expel("+count+")") ; 41 42 Object [] keys = new Object [count] ; 43 for ( int i = 0 ; i < count ; i++ ) 44 { 45 keys[i] = list.removeLast() ; 46 if ( log.isTraceEnabled() ) 47 log.trace("expel "+keys[i]) ; 48 } 49 return keys ; 50 } 51 52 53 public void update(Object key) 54 { 55 if ( log.isTraceEnabled() ) 56 log.trace("update("+key+")") ; 57 if ( ! list.getFirst().equals(key) ) 58 { 59 list.remove(key) ; 60 list.addFirst(key) ; 61 } 62 63 if ( log.isTraceEnabled() ) 64 dump() ; 65 } 66 67 68 public void newItem(Object key, Object value) 69 { 70 if ( log.isTraceEnabled() ) 71 log.trace("newItem("+key+")") ; 72 list.addFirst(key) ; 73 } 74 75 private void dump() 76 { 77 String $state = "" ; 78 for ( Iterator iter = list.listIterator() ; iter.hasNext() ; ) 79 { 80 $state = $state+iter.next()+" " ; 81 } 82 log.trace($state) ; 83 } 84 } 85 86 87 113 | Popular Tags |