1 8 package org.apache.avalon.excalibur.cache.policy; 9 10 import java.util.HashMap ; 11 import java.util.TreeMap ; 12 import org.apache.avalon.excalibur.cache.ReplacementPolicy; 13 14 22 public class TimeMapLRUPolicy 23 implements ReplacementPolicy 24 { 25 26 protected TreeMap m_timeToKeyMap; 27 protected HashMap m_keyToTimeMap; 28 29 private long m_time; 30 private Object m_lock; 31 32 public TimeMapLRUPolicy() 33 { 34 m_timeToKeyMap = new TreeMap (); 35 m_keyToTimeMap = new HashMap (); 36 m_time = 0; 37 m_lock = new Object (); 38 } 39 40 public void hit( final Object key ) 41 { 42 remove( key ); 43 add( key ); 44 } 45 46 public void add( final Object key ) 47 { 48 Long time = getTime(); 49 m_timeToKeyMap.put( time, key ); 50 m_keyToTimeMap.put( key, time ); 51 } 52 53 public void remove( final Object key ) 54 { 55 Long time = (Long )m_keyToTimeMap.remove( key ); 56 m_timeToKeyMap.remove( time ); 57 } 58 59 public Object selectVictim() 60 { 61 Object time = m_timeToKeyMap.firstKey(); 62 Object key = m_timeToKeyMap.get( time ); 63 return key; 64 } 65 66 private Long getTime() 67 { 68 synchronized ( m_lock ) 69 { 70 return new Long ( m_time++ ); 71 } 72 } 73 } 74 | Popular Tags |