T
- The reference type stored in the cache.public class HardReferenceQueue<T> extends RingBuffer<T> implements IHardReferenceQueue<T>
A cache for hard references using an LRU policy. References are simply
cached, but objects are not recoverable from their reference. In order to
make an object recoverable, this cache must be wrapped by a weak reference
cache that implements a hash map for discovery of objects using their
persistent identifier. The HardReferenceQueue
has a capacity that
determines the #of hard references that may be cached before an object is
evicted from the cache. Objects may be stored multiple times on the cache but
the nscan most recent references are always tested before appending a
reference to the cache in order to minimize cache churn when an object is
touched repeatedly in close succession. Likewise, eviction does not mean that
the object is no longer on the hard reference cache, nor does eviction mean
that no hard references to the object exist within the VM. However, eviction
is nevertheless used to drive persistence of the object. Since an object may
be evicted multiple times without being updated in between evictions, this
requires the object to implement a protocol for determining whether or not it
is dirty. Eviction is then contingent on that protocol and the dirty state of
the object is reset when it is serialized for eviction. In combination with
the object's dirty protocol, the hard reference cache can substitute for a
commit list.
Note: This implementation is NOT synchronized.
Modifier and Type | Field and Description |
---|---|
protected int |
nscan
The #of references to scan backwards from the LRU position when testing
for whether or not a reference is already in the cache.
|
capacity, size
DEFAULT_NSCAN
Constructor and Description |
---|
HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
int capacity)
Uses the default #of references to scan on append requests.
|
HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener,
int capacity,
int nscan)
Fully specified ctor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T ref)
Add a reference to the cache.
|
protected void |
beforeOffer(T ref)
Extended to evict the element at the tail of the buffer iff the buffer is
full.
|
boolean |
evict()
Evict the LRU reference.
|
void |
evictAll(boolean clearRefs)
Evict all references, starting with the LRU reference and proceeding to
the MRU reference.
|
HardReferenceQueueEvictionListener<T> |
getListener()
The listener specified to the constructor.
|
int |
nscan()
The #of references that are tested on append requests.
|
_get, addAll, capacity, clear, clear, contains, containsAll, element, get, isEmpty, isFull, iterator, offer, peek, poll, remove, remove, removeAll, retainAll, scanHead, scanTail, size, toArray, toArray
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
capacity, clear, isEmpty, isFull, peek, size
equals, hashCode
protected final int nscan
public HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity)
listener
- The listener on which cache evictions are reported.capacity
- The maximum #of references that can be stored on the cache.
There is no guarantee that all stored references are distinct.public HardReferenceQueue(HardReferenceQueueEvictionListener<T> listener, int capacity, int nscan)
listener
- The listener on which cache evictions are reported (optional).capacity
- The maximum #of references that can be stored on the cache.
There is no guarantee that all stored references are distinct.nscan
- The #of references to scan from the MRU position before
appended a reference to the cache. Scanning is used to reduce
the chance that references that are touched several times in
near succession from entering the cache more than once. The
#of reference tests trades off against the latency of adding a
reference to the cache.public final HardReferenceQueueEvictionListener<T> getListener()
public final int nscan()
nscan
in interface IHardReferenceQueue<T>
public boolean add(T ref)
add
in interface IHardReferenceQueue<T>
add
in interface Collection<T>
add
in interface Queue<T>
add
in class RingBuffer<T>
ref
- The reference to be added.protected void beforeOffer(T ref)
Note: This hook is further extended to realize the stale reference
protocol in SynchronizedHardReferenceQueueWithTimeout
.
beforeOffer
in class RingBuffer<T>
public final boolean evict()
evict
in interface IHardReferenceQueue<T>
HardReferenceQueueEvictionListener
public final void evictAll(boolean clearRefs)
evictAll
in interface IHardReferenceQueue<T>
clearRefs
- When true, the reference are actually cleared from the cache.
This may be false to force persistence of the references in
the cache without actually clearing the cache.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.