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
30 public class CachedObject<KeyType extends Serializable, ValueType extends Serializable> {
31 private static final Logger LOGGER = Logger.getLogger(CachedObject.class
37 private Cache<KeyType, ValueType> cache;
40 * Key of the object in the cache.
42 private KeyType objectKey;
47 private ValueType value;
50 * Are we now computing the value or not?
52 private boolean computing;
55 * Computation used to obtain the object if it is not found in the cache.
57 private Computation<KeyType, ValueType> computation;
60 * Constructs the cached object.
65 * Key of the object in the cache.
67 * Computation to get the object in case the object is not in the
70 public CachedObject(Cache<KeyType, ValueType> aCache, KeyType aObjectKey,
71 Computation<KeyType, ValueType> aComputation) {
73 objectKey = aObjectKey;
74 computation = aComputation;
78 * Gets the object. Since the object is cached, different calls to this
79 * method may return different objects.
81 * If the object is expired from the cache it is recomputed using the
82 * callback. In case the callback throws an exception the last known value
83 * is used. In case an exception is thrown, the problem is also logged. In
84 * case a recomputation is already being done by another thread, the last
85 * known value is immediately returned.
89 public ValueType get() {
92 // always return old value while computing.
96 ValueType cachedValue = cache.get(objectKey);
97 if (cachedValue == null) {
107 // we only get here if we are computing
108 // do the computation without holding the lock.
109 LOGGER.fine("Refreshing cache for '" + objectKey + "'");
110 ValueType object = computation.getObject(objectKey);
111 cache.put(objectKey, object);
113 synchronized (this) {
116 } catch (Exception e) {
117 LOGGER.log(Level.INFO,
118 "Recomputation of cached item failed for key '" + objectKey +
121 synchronized (this) {
125 synchronized (this) {
131 * Invalidates the cache for the object so that it is recomputed the next
132 * time it is requested.
134 public void invalidate() {
135 cache.remove(objectKey);
143 public Cache getCache() {
148 * Callback invoked to compute an object if it was not found in the cache.
153 public static interface Computation<Key extends Serializable, Value extends Serializable> {
155 * Gets the object. Called when the object is not in the cache. In case
156 * computation fails, an exception should be thrown to ensure that the
157 * last known value will be used.
160 * Id of the object in the cache.
162 * @return Object, must be non-null.
164 Value getObject(Key aObjectKey) throws Exception;