public abstract class CachingReference<E>
extends java.lang.Object
Object that it holds and handle the renewal of this object upon expiration.
Greatly inspired by the CachedData sample provided in the javadoc
of ReentrantReadWriteLock.
Object is created implementing the newObject() method.
Sample to get an InetAddress refreshed against a DNS every 10 seconds:
CachingReference myRemoteServerAddress = new CachingReference<InetAddress>(10, TimeUnit.SECONDS) {
protected InetAddress newObject() {
try {
return InetAddress.getByName(myRemoteServerHostname);
} catch () {
throw new RuntimeException("Exception resolving '" + myRemoteServerHostname + "'", e);
}
}
}
| Constructor and Description |
|---|
CachingReference(long timeToLiveInNanos) |
CachingReference(long timeToLive,
java.util.concurrent.TimeUnit timeToLiveUnit) |
| Modifier and Type | Method and Description |
|---|---|
E |
get() |
protected abstract E |
newObject() |
void |
purge()
Purge the cached reference so that a new object will be created for the next
get() |
java.lang.String |
toString() |
public CachingReference(long timeToLiveInNanos)
public CachingReference(long timeToLive,
java.util.concurrent.TimeUnit timeToLiveUnit)
@Nullable protected abstract E newObject()
@Nullable public E get()
Object hold by this reference.public void purge()
get()public java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2018. All Rights Reserved.