org.jboss.cache.eviction
Class ExpirationAlgorithm

java.lang.Object
  extended by org.jboss.cache.eviction.BaseEvictionAlgorithm
      extended by org.jboss.cache.eviction.ExpirationAlgorithm
All Implemented Interfaces:
EvictionAlgorithm

public class ExpirationAlgorithm
extends BaseEvictionAlgorithm

Eviction algorithm that uses a key in the Node data that indicates the time the node should be evicted. The key must be a java.lang.Long object, with the time to expire as milliseconds past midnight January 1st, 1970 UTC (the same relative time as provided by System.currentTimeMillis()).

This algorithm also obeys the configuration key EvictionPolicyConfigBase.getMaxNodes(), and will evict the soonest to expire entires first to reduce the region size. If there are not enough nodes with expiration keys set, a warning is logged.

If a node in the eviction region does not have an expiration value, then ExpirationConfiguration.getTimeToLiveSeconds() (if set) will be used. The expiration is updated when a node is added or updated.

If there is no time-to-live set, and a node in the eviction region does not have an expiration value, then that node will never be evicted. As forgetting to indicate an expiration value is likely a mistake, a warning message is logged by this class. This warning, however, can be disabled through ExpirationConfiguration.setWarnNoExpirationKey(boolean).

A node's expiration time can be changed by setting a new value in the node.

Example usage:

 Cache cache;
 Fqn fqn1 = Fqn.fromString("/node/1");
 Long future = new Long(System.currentTimeMillis() + 2000);
 cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future);
 cache.put(fqn1, "foo");
 assertTrue(cache.get(fqn1) != null);
 

Thread.sleep(5000); // 5 seconds assertTrue(cache.get(fqn1) == null);


Field Summary
 
Fields inherited from class org.jboss.cache.eviction.BaseEvictionAlgorithm
allowTombstones, evictionQueue, recycleQueue, region
 
Constructor Summary
ExpirationAlgorithm(ExpirationPolicy policy)
          Constructs a new algorithm with a policy.
 
Method Summary
protected  void processQueues(Region region)
          Event processing for Evict/Add/Visiting of nodes.
protected  void prune()
           
 void resetEvictionQueue(Region region)
          Reset the whole eviction queue.
protected  EvictionQueue setupEvictionQueue(Region region)
          This method will create an EvictionQueue implementation and prepare it for use.
protected  boolean shouldEvictNode(NodeEntry ne)
          This method will check whether the given node should be evicted or not.
 
Methods inherited from class org.jboss.cache.eviction.BaseEvictionAlgorithm
emptyRecycleQueue, evict, evictCacheNode, getEvictionQueue, initialize, isNodeInUseAndNotTimedOut, isYoungerThanMinimumTimeToLive, process, processAddedElement, processAddedNodes, processAddedNodes, processMarkInUseNodes, processRemovedElement, processRemovedNodes, processUnmarkInUseNodes, processVisitedNodes, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExpirationAlgorithm

public ExpirationAlgorithm(ExpirationPolicy policy)
Constructs a new algorithm with a policy.

Method Detail

processQueues

protected void processQueues(Region region)
                      throws EvictionException
Description copied from class: BaseEvictionAlgorithm
Event processing for Evict/Add/Visiting of nodes.

- On AddEvents a new element is added into the eviction queue - On RemoveEvents, the removed element is removed from the eviction queue. - On VisitEvents, the visited node has its eviction statistics updated (idleTime, numberOfNodeVisists, etc..)

Overrides:
processQueues in class BaseEvictionAlgorithm
Parameters:
region - Cache region to process for eviction.
Throws:
EvictionException

prune

protected void prune()
              throws EvictionException
Overrides:
prune in class BaseEvictionAlgorithm
Throws:
EvictionException

resetEvictionQueue

public void resetEvictionQueue(Region region)
Description copied from interface: EvictionAlgorithm
Reset the whole eviction queue. Queue may needs to be reset due to corrupted state, for example.

Specified by:
resetEvictionQueue in interface EvictionAlgorithm
Overrides:
resetEvictionQueue in class BaseEvictionAlgorithm
Parameters:
region - MarshRegion that this algorithm will operate on.

setupEvictionQueue

protected EvictionQueue setupEvictionQueue(Region region)
                                    throws EvictionException
Description copied from class: BaseEvictionAlgorithm
This method will create an EvictionQueue implementation and prepare it for use.

Specified by:
setupEvictionQueue in class BaseEvictionAlgorithm
Parameters:
region - MarshRegion to setup an eviction queue for.
Returns:
The created EvictionQueue to be used as the eviction queue for this algorithm.
Throws:
EvictionException
See Also:
EvictionQueue

shouldEvictNode

protected boolean shouldEvictNode(NodeEntry ne)
Description copied from class: BaseEvictionAlgorithm
This method will check whether the given node should be evicted or not.

Specified by:
shouldEvictNode in class BaseEvictionAlgorithm
Parameters:
ne - NodeEntry to test eviction for.
Returns:
True if the given node should be evicted. False if the given node should not be evicted.


Copyright © 2008 JBoss, a division of Red Hat. All Rights Reserved.