2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.cache;
18 import java.io.Serializable;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
23 * Represents a cached object identified by the key it has in a certain
24 * {@link Cache}. The object is either retrieved from the cache if the cache has
25 * it, or a call back is invoked to get the object (and put it in the cache).
27 * @author Erik Brakkee
29 public class CachedObject<KeyType extends Serializable, ValueType extends Serializable> {
30 private static final Logger LOGGER = Logger.getLogger(CachedObject.class.getName());
35 private Cache<KeyType, ValueType> cache;
38 * Key of the object in the cache.
40 private KeyType objectKey;
43 * Last known value. We only use this to return the last known value in case recomputation of the value fails.
45 private ValueType value;
48 * Are we now computing the value or not?
50 private boolean computing;
53 * Computation used to obtain the object if it is not found in the cache.
55 private Computation<KeyType, ValueType> computation;
58 * Constructs the cached object.
60 * @param aCache Cache to use.
61 * @param aObjectKey Key of the object in the cache.
62 * @param aComputation Computation to get the object in case the object is not in the
65 public CachedObject(Cache<KeyType, ValueType> aCache, KeyType aObjectKey,
66 Computation<KeyType, ValueType> aComputation) {
68 objectKey = aObjectKey;
69 computation = aComputation;
73 * Gets the object. Since the object is cached, different calls to this
74 * method may return different objects.
76 * If the object is expired from the cache it is recomputed using the
77 * callback. In case the callback throws an exception the last known value
78 * is used. In case an exception is thrown, the problem is also logged. In
79 * case a recomputation is already being done by another thread, the last
80 * known value is immediately returned.
84 public ValueType get() {
87 // always return old value while computing.
91 ValueType cachedValue = cache.get(objectKey);
92 if (cachedValue == null) {
96 // Two different instances of cached object might share the same cache and so it can occur
97 // that the value in one of the instances it out of date.
104 // we only get here if we are computing
105 // do the computation without holding the lock.
106 LOGGER.fine("Refreshing cache for '" + objectKey + "'");
107 ValueType object = computation.getObject(objectKey);
108 cache.put(objectKey, object);
110 synchronized (this) {
114 catch (Exception e) {
115 LOGGER.log(Level.INFO, "Recomputation of cached item failed for key '" + objectKey +
118 synchronized (this) {
122 synchronized (this) {
128 * Invalidates the cache for the object so that it is recomputed the next
129 * time it is requested.
131 public void invalidate() {
132 cache.remove(objectKey);
140 public Cache getCache() {
145 * Callback invoked to compute an object if it was not found in the cache.
147 * @param <T> Type of the object
149 public static interface Computation<Key extends Serializable, Value extends Serializable> {
151 * Gets the object. Called when the object is not in the cache. In case
152 * computation fails, an exception should be thrown to ensure that the
153 * last known value will be used.
155 * @param aObjectKey Id of the object in the cache.
156 * @return Object, must be non-null.
158 Value getObject(Key aObjectKey) throws Exception;