/** Looks up an entry. creating it if not found. */ public Element get(final Object key) throws LockTimeoutException { try { // if null will lock here Element element = super.get(key); if (element == null) { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); put(element); } return element; } catch (LockTimeoutException e) { // do not release the lock, because you never acquired it String message = "Timeout after " + timeoutMillis + " waiting on another thread " + "to fetch object for cache entry \"" + key + "\"."; throw new LockTimeoutException(message, e); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired put(new Element(key, null)); throw new CacheException( "Could not fetch object for cache entry with key \"" + key + "\".", throwable); } }
/** * Refresh a single element. * * @param element the Element to refresh * @param backingCache the underlying {@link Ehcache}. * @param quiet whether to use putQuiet or not, if true replication will not occur * @return the refreshed Element * @throws Exception * @since 1.6.1 */ protected Element refreshElement(final Element element, Ehcache backingCache, boolean quiet) throws Exception { Object key = element.getObjectKey(); if (LOG.isLoggable(Level.FINE)) { LOG.fine(getName() + ": refreshing element with key " + key); } final Element replacementElement; if (factory instanceof UpdatingCacheEntryFactory) { // update the value of the cloned Element in place replacementElement = element; ((UpdatingCacheEntryFactory) factory) .updateEntryValue(key, replacementElement.getObjectValue()); // put the updated element back into the backingCache, without updating stats // It is not usually necessary to do this. We do this in case the element expired // or idles out of the backingCache. In that case we hold a reference to it but the // backingCache no longer does. } else { final Object value = factory.createEntry(key); replacementElement = makeAndCheckElement(key, value); } if (quiet) { backingCache.putQuiet(replacementElement); } else { backingCache.put(replacementElement); } return replacementElement; }