2
* Copyright 2003-2009 Terracotta, Inc.
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.
17
package net.sf.ehcache.constructs.blocking;
20
import net.sf.ehcache.CacheException;
21
import net.sf.ehcache.Ehcache;
22
import net.sf.ehcache.Element;
23
import net.sf.ehcache.concurrent.Mutex;
25
import java.util.logging.Level;
26
import java.util.logging.Logger;
30
* A {@link net.sf.ehcache.Cache} backed cache that creates entries on demand.
32
* Clients of the cache simply call it without needing knowledge of whether
33
* the entry exists in the cache, or whether it needs updating before use.
36
* Thread safety depends on the factory being used. The UpdatingCacheEntryFactory should be made
37
* thread safe. In addition users of returned values should not modify their contents.
40
* @version $Id: UpdatingSelfPopulatingCache.java 1012 2009-08-20 04:23:00Z gregluck $
42
public class UpdatingSelfPopulatingCache extends SelfPopulatingCache {
44
private static final Logger LOG = Logger.getLogger(UpdatingSelfPopulatingCache.class.getName());
47
* Creates a SelfPopulatingCache.
49
public UpdatingSelfPopulatingCache(Ehcache cache, final UpdatingCacheEntryFactory factory)
50
throws CacheException {
51
super(cache, factory);
57
* If null, it creates it. If not null, it updates it. For performance this method should only be
58
* used with {@link UpdatingCacheEntryFactory}'s
61
* gets, which update as part of the get, might take considerable time. Access to the cache cannot be blocked
62
* while that is happening. This method is therefore not synchronized. Mutexes are used for thread safety based on key
66
* @throws net.sf.ehcache.CacheException
68
public Element get(final Object key) throws LockTimeoutException {
72
Ehcache backingCache = getCache();
73
Element element = backingCache.get(key);
75
if (element == null) {
76
element = super.get(key);
78
Mutex lock = stripedMutex.getMutexForKey(key);
87
} catch (final Throwable throwable) {
88
// Could not fetch - Ditch the entry from the cache and rethrow
89
put(new Element(key, null));
90
throw new LockTimeoutException("Could not update object for cache entry with key \"" + key + "\".", throwable);
95
* Element can never be null. Add a null guard just in case.
98
protected void update(final Object key) {
100
Ehcache backingCache = getCache();
101
final Element element = backingCache.getQuiet(key);
103
if (element == null) {
104
if (LOG.isLoggable(Level.FINE)) {
105
LOG.fine(getName() + ": entry with key " + key + " has been removed - skipping it");
110
refreshElement(element, backingCache);
111
} catch (final Exception e) {
112
// Collect the exception and keep going.
113
// Throw the exception once all the entries have been refreshed
114
// If the refresh fails, keep the old element. It will simply become staler.
115
LOG.log(Level.WARNING, getName() + "Could not refresh element " + key, e);
120
* This method should not be used. Because elements are always updated before they are
121
* returned, it makes no sense to refresh this cache.
123
public void refresh() throws CacheException {
124
throw new CacheException("UpdatingSelfPopulatingCache objects should not be refreshed.");