JBoss.org Community Documentation
org.jboss.cache.eviction.ExpirationPolicy
is a policy
that evicts nodes based on an absolute expiration time. The
expiration time is indicated using the
org.jboss.cache.Node.put()
method, using a String key
expiration
and the absolute time as a
java.lang.Long
object, with a value indicated as milliseconds past midnight
January 1st, 1970 UTC (the same relative time as provided by
java.lang.System.currentTimeMillis()
).
This policy guarantees a constant order (
O (1)
) for adds and removals.
Internally, a sorted set (TreeSet) containing the expiration
time and Fqn of the nodes is stored, which essentially
functions as a heap.
This policy has the following configuration parameters:
expirationKeyName
- This is the Node key name used
in the eviction algorithm. The configuration default is
expiration
.
maxNodes
- This is the maximum number of nodes allowed in this region. 0 denotes no limit.
The following listing shows how the expiration date is indicated and how the policy is applied:
Cache cache = DefaultCacheFactory.createCache();
Fqn fqn1 = Fqn.fromString("/node/1");
Long future = new Long(System.currentTimeMillis() + 2000);
// sets the expiry time for a node
cache.getRoot().addChild(fqn1).put(ExpirationConfiguration.EXPIRATION_KEY, future);
assertTrue(cache.getRoot().hasChild(fqn1));
Thread.sleep(5000);
// after 5 seconds, expiration completes
assertFalse(cache.getRoot().hasChild(fqn1));
Note that the expiration time of nodes is only checked when the
region manager wakes up every
wakeUpIntervalSeconds
, so eviction
may happen a few seconds later than indicated.