1 8 package org.jboss.cache.eviction; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.jboss.cache.Region; 13 14 import java.util.Iterator ; 15 16 23 public class LRUAlgorithm extends BaseEvictionAlgorithm implements EvictionAlgorithm 24 { 25 private static final Log log = LogFactory.getLog(LRUAlgorithm.class); 26 27 public LRUAlgorithm() 28 { 29 super(); 30 } 31 32 protected EvictionQueue setupEvictionQueue(Region region) throws EvictionException 33 { 34 return new LRUQueue(); 35 } 36 37 protected boolean shouldEvictNode(NodeEntry entry) 38 { 39 LRUConfiguration config = (LRUConfiguration) region.getEvictionPolicyConfig(); 40 if (config.getTimeToLiveSeconds() == 0 && config.getMaxAgeSeconds() == 0) return false; 42 43 long currentTime = System.currentTimeMillis(); 44 if (config.getTimeToLiveSeconds() != 0) 45 { 46 long idleTime = currentTime - entry.getModifiedTimeStamp(); 47 if (log.isTraceEnabled()) 48 { 49 log.trace("Node " + entry.getFqn() + " has been idle for " + idleTime + "ms"); 50 } 51 if ((idleTime >= (config.getTimeToLiveSeconds() * 1000))) 52 { 53 if (log.isTraceEnabled()) 54 { 55 log.trace("Node " + entry.getFqn() + " should be evicted because of idle time"); 56 } 57 return true; 58 } 59 } 60 61 if (config.getMaxAgeSeconds() != 0) 62 { 63 long objectLifeTime = currentTime - entry.getCreationTimeStamp(); 64 if (log.isTraceEnabled()) 65 { 66 log.trace("Node " + entry.getFqn() + " has been alive for " + objectLifeTime + "ms"); 67 } 68 if ((objectLifeTime >= (config.getMaxAgeSeconds() * 1000))) 69 { 70 if (log.isTraceEnabled()) 71 { 72 log.trace("Node " + entry.getFqn() + " should be evicted because of max age"); 73 } 74 return true; 75 } 76 } 77 78 if (log.isTraceEnabled()) 79 { 80 log.trace("Node " + entry.getFqn() + " should not be evicted"); 81 } 82 return false; 83 } 84 85 protected void evict(NodeEntry ne) 86 { 87 if (ne != null) 89 { 90 if (!this.evictCacheNode(ne.getFqn())) 92 { 93 try 94 { 95 recycleQueue.put(ne.getFqn()); 96 } 97 catch (InterruptedException e) 98 { 99 log.debug("InterruptedException", e); 100 } 101 } 102 } 103 } 104 105 protected void prune() throws EvictionException 106 { 107 LRUQueue lruQueue = (LRUQueue) evictionQueue; 108 NodeEntry ne; 109 Iterator it = lruQueue.iterateLRUQueue(); 110 while (it.hasNext()) 111 { 112 ne = (NodeEntry) it.next(); 113 if (isNodeInUseAndNotTimedOut(ne)) 114 { 115 continue; 116 } 117 118 if (this.shouldEvictNode(ne)) 119 { 120 it.remove(); 121 lruQueue.removeNodeEntryFromMaxAge(ne); 122 this.evict(ne); 123 } 124 else 125 { 126 break; 127 } 128 } 129 130 it = lruQueue.iterateMaxAgeQueue(); 131 while (it.hasNext()) 132 { 133 ne = (NodeEntry) it.next(); 134 if (isNodeInUseAndNotTimedOut(ne)) 135 { 136 continue; 137 } 138 139 if (this.shouldEvictNode(ne)) 140 { 141 it.remove(); 142 lruQueue.removeNodeEntryFromLRU(ne); 143 this.evict(ne); 144 } 145 else 146 { 147 break; 148 } 149 } 150 151 int maxNodes = this.getConfiguration().getMaxNodes(); 152 if (maxNodes <= 0) 153 { 154 return; 155 } 156 157 it = lruQueue.iterateLRUQueue(); 158 while (evictionQueue.getNumberOfNodes() > maxNodes) 159 { 160 ne = (NodeEntry) it.next(); 161 if (log.isTraceEnabled()) 162 { 163 log.trace("Node " + ne.getFqn() + " will be evicted because of exceeding the maxNode limit." + 164 " maxNode: " + maxNodes + " but current queue size is: " + evictionQueue.getNumberOfNodes()); 165 } 166 167 if (!this.isNodeInUseAndNotTimedOut(ne)) 168 { 169 it.remove(); 170 lruQueue.removeNodeEntryFromMaxAge(ne); 171 this.evict(ne); 172 } 173 } 174 } 175 176 protected LRUConfiguration getConfiguration() 177 { 178 return (LRUConfiguration) region.getEvictionPolicyConfig(); 179 } 180 181 } 182 | Popular Tags |