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 EvictionAlgorithmConfigBase.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 ExpirationAlgorithmConfig.getTimeToLive() (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 ExpirationAlgorithmConfig.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, cache, configuration, evictionActionPolicy, evictionAlgorithmConfig, evictionQueue, recycleQueue, regionFqn
 
Constructor Summary
ExpirationAlgorithm()
          Constructs a new algorithm with a policy.
 
Method Summary
 boolean canIgnoreEvent(EvictionEvent.Type eventType)
          Tests whether the algorithm would ignore certain event types on certain Fqns.
 Class<? extends EvictionAlgorithmConfig> getConfigurationClass()
          This is a helper so that the XML parser will be able to select and use the correct EvictionAlgorithmConfig implementation class corresponding to this EvictionAlgorithm.
protected  void processQueues(BlockingQueue<EvictionEvent> queue)
          Event processing for Evict/Add/Visiting of nodes.
protected  void prune()
           
 void resetEvictionQueue()
          Reset the whole eviction queue.
protected  EvictionQueue setupEvictionQueue()
          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
assignToRegion, emptyRecycleQueue, evict, evictCacheNode, getEvictionActionPolicy, getEvictionAlgorithmConfig, getEvictionQueue, getNextInQueue, initialize, isNodeInUseAndNotTimedOut, isYoungerThanMinimumTimeToLive, process, processAddedElement, processAddedNodes, processAddedNodes, processMarkInUseNodes, processRemovedElement, processRemovedNodes, processUnmarkInUseNodes, processVisitedNodes, setEvictionActionPolicy, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExpirationAlgorithm

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

Method Detail

processQueues

protected void processQueues(BlockingQueue<EvictionEvent> queue)
                      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:
queue - queue to inspect
Throws:
EvictionException - in the event of problems

prune

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

resetEvictionQueue

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

Specified by:
resetEvictionQueue in interface EvictionAlgorithm
Overrides:
resetEvictionQueue in class BaseEvictionAlgorithm

setupEvictionQueue

protected EvictionQueue setupEvictionQueue()
                                    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
Returns:
The created EvictionQueue to be used as the eviction queue for this algorithm.
Throws:
EvictionException - if there are problems
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.

canIgnoreEvent

public boolean canIgnoreEvent(EvictionEvent.Type eventType)
Description copied from interface: EvictionAlgorithm
Tests whether the algorithm would ignore certain event types on certain Fqns.

Specified by:
canIgnoreEvent in interface EvictionAlgorithm
Overrides:
canIgnoreEvent in class BaseEvictionAlgorithm
Parameters:
eventType - event type to test for
Returns:
true if the event representing the parameters would be ignored by this algorithm or not.

getConfigurationClass

public Class<? extends EvictionAlgorithmConfig> getConfigurationClass()
Description copied from interface: EvictionAlgorithm
This is a helper so that the XML parser will be able to select and use the correct EvictionAlgorithmConfig implementation class corresponding to this EvictionAlgorithm. E.g., the FIFOAlgorithm would return FIFOAlgorithmConfig.class.

Returns:
a class that is used to configure this EvictionAlgorithm.


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