- 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 {