org.wamblee.cache
Class CachedObject<KeyType extends Serializable,ValueType extends Serializable>

java.lang.Object
  extended by org.wamblee.cache.CachedObject<KeyType,ValueType>

public class CachedObject<KeyType extends Serializable,ValueType extends Serializable>
extends Object

Represents a cached object identified by the key it has in a certain Cache. The object is either retrieved from the cache if the cache has it, or a call back is invoked to get the object (and put it in the cache).

Author:
Erik Brakkee

Nested Class Summary
static interface CachedObject.Computation<Key extends Serializable,Value extends Serializable>
          Callback invoked to compute an object if it was not found in the cache.
 
Constructor Summary
CachedObject(Cache<KeyType,ValueType> aCache, KeyType aObjectKey, CachedObject.Computation<KeyType,ValueType> aComputation)
          Constructs the cached object.
 
Method Summary
 ValueType get()
          Gets the object.
 Cache getCache()
          Gets the cache.
 void invalidate()
          Invalidates the cache for the object so that it is recomputed the next time it is requested.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedObject

public CachedObject(Cache<KeyType,ValueType> aCache,
                    KeyType aObjectKey,
                    CachedObject.Computation<KeyType,ValueType> aComputation)
Constructs the cached object.

Parameters:
aCache - Cache to use.
aObjectKey - Key of the object in the cache.
aComputation - Computation to get the object in case the object is not in the cache.
Method Detail

get

public ValueType get()
Gets the object. Since the object is cached, different calls to this method may return different objects. If the object is expired from the cache it is recomputed using the callback. In case the callback throws an exception the last known value is used. In case an exception is thrown, the problem is also logged. In case a recomputation is already being done by another thread, the last known value is immediately returned.

Returns:
Object.

invalidate

public void invalidate()
Invalidates the cache for the object so that it is recomputed the next time it is requested.


getCache

public Cache getCache()
Gets the cache.

Returns:
Cache.


Copyright © 2011. All Rights Reserved.