X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fcache%2FCachedObject.java;h=7ec87534f08e521ec5c95bcccc6ec4b0479277a0;hb=02f5f47eb96320baf55a6cb2e5141360e304b504;hp=58daf8c918ee3c53ff4ade7d31db0ee9d90fe279;hpb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;p=utils diff --git a/support/general/src/main/java/org/wamblee/cache/CachedObject.java b/support/general/src/main/java/org/wamblee/cache/CachedObject.java index 58daf8c9..7ec87534 100644 --- a/support/general/src/main/java/org/wamblee/cache/CachedObject.java +++ b/support/general/src/main/java/org/wamblee/cache/CachedObject.java @@ -1,5 +1,5 @@ /* - * Copyright 2005 the original author or authors. + * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,55 +13,48 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.wamblee.cache; import java.io.Serializable; - -import org.apache.log4j.Logger; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Represents a cached object. 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). - * + * Represents a cached object identified by the key it has in a certain + * {@link 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 + * */ public class CachedObject { + private static final Logger LOGGER = Logger.getLogger(CachedObject.class + .getName()); - private static final Logger LOGGER = Logger.getLogger(CachedObject.class); + /** + * Cache to use. + */ + private Cache cache; /** - * Callback invoked to compute an object if it was not found in the cache. - * - * @param - * Type of the object + * Key of the object in the cache. */ - public static interface Computation { - /** - * Gets the object. Called when the object is not in the cache. - * - * @param aObjectKey - * Id of the object in the cache. - * @return Object, must be non-null. - */ - Value getObject(Key aObjectKey); - } + private KeyType objectKey; /** - * Cache to use. + * Last known value. */ - private Cache _cache; + private ValueType value; /** - * Key of the object in the cache. + * Are we now computing the value or not? */ - private KeyType _objectKey; + private boolean computing; /** * Computation used to obtain the object if it is not found in the cache. */ - private Computation _computation; + private Computation computation; /** * Constructs the cached object. @@ -75,49 +68,71 @@ public class CachedObject aCache, KeyType aObjectKey, - Computation aComputation) { - _cache = aCache; - _objectKey = aObjectKey; - _computation = aComputation; + Computation aComputation) { + cache = aCache; + objectKey = aObjectKey; + computation = aComputation; } /** * 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. + * * @return Object. */ public ValueType get() { - ValueType object = (ValueType) _cache.get(_objectKey); // the used - // cache is - // thread safe. - if (object == null) { - // synchronize the computation to make sure that the object is only - // computed - // once when multiple concurrent threads detect that the entry must - // be - // recomputed. + synchronized (this) { + if (computing) { + // always return old value while computing. + return value; + } + + ValueType cachedValue = cache.get(objectKey); + if (cachedValue == null) { + // expired + computing = true; + } else { + // no change. + return value; + } + } + try { + + // we only get here if we are computing + // do the computation without holding the lock. + LOGGER.fine("Refreshing cache for '" + objectKey + "'"); + ValueType object = computation.getObject(objectKey); + cache.put(objectKey, object); + + synchronized (this) { + value = object; + } + } catch (Exception e) { + LOGGER.log(Level.INFO, + "Recomputation of cached item failed for key '" + objectKey + + "'", e); + } finally { synchronized (this) { - object = (ValueType) _cache.get(_objectKey); - if (object == null) { - // No other thread did a recomputation so we must do this - // now. - LOGGER.debug("Refreshing cache for '" + _objectKey + "'"); - object = _computation.getObject(_objectKey); - _cache.put(_objectKey, object); - } + computing = false; } } - return object; + synchronized(this) { + return value; + } } /** * Invalidates the cache for the object so that it is recomputed the next * time it is requested. - * */ public void invalidate() { - _cache.remove(_objectKey); + cache.remove(objectKey); } /** @@ -126,6 +141,26 @@ public class CachedObject + * Type of the object + */ + public static interface Computation { + /** + * Gets the object. Called when the object is not in the cache. + * In case computation fails, an exception should be thrown to ensure that the last + * known value will be used. + * + * @param aObjectKey + * Id of the object in the cache. + * + * @return Object, must be non-null. + */ + Value getObject(Key aObjectKey) throws Exception; } }