/** * Gets an element from the cache. Updates Element Statistics * <p> * Note that the Element's lastAccessTime is always the time of this get. * Use {@link #getQuiet(Object)} to peak into the Element to see its last access time with get * * @param key a serializable value * @return the element, or null, if it does not exist. * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE} * @see #isExpired */ @Override public Element get(Serializable key) throws IllegalStateException, CacheException { return this.get((Object) key); }
try { checkNoReentry(request); Element element = blockingCache.get(key); if (element == null || element.getObjectValue() == null) { try {
@Override public StoreEntry get(Long key) { Element entry = null; try { /* This may block. */ entry = cache.get(key); } catch (LockTimeoutException e) { throw new RuntimeException(); } catch (RuntimeException e) { /* Release the lock that may have been acquired. */ cache.put(new Element(key, null)); } StoreEntry result = null; if (entry != null) { /* * We don't need to check isExpired() on the result, since ehcache takes care of expiring entries for us. * c.f. the get(Key) implementation in this class. */ result = (StoreEntry)entry.getObjectValue(); } return result; }
/** * Looks up an entry. creating it if not found. */ @Override public Element get(final Object key) throws LockTimeoutException { Element element = super.get(key); if (element == null) { try { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired element = new Element(key, null); throw new CacheException("Could not fetch object for cache entry with key \"" + key + "\".", throwable); } finally { put(element); } } return element; }
/** * Gets an element from the cache. Updates Element Statistics * <p> * Note that the Element's lastAccessTime is always the time of this get. * Use {@link #getQuiet(Object)} to peak into the Element to see its last access time with get * * @param key a serializable value * @return the element, or null, if it does not exist. * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE} * @see #isExpired */ @Override public Element get(Serializable key) throws IllegalStateException, CacheException { return this.get((Object) key); }
/** * Gets an element from the cache. Updates Element Statistics * <p/> * Note that the Element's lastAccessTime is always the time of this get. * Use {@link #getQuiet(Object)} to peak into the Element to see its last access time with get * * @param key a serializable value * @return the element, or null, if it does not exist. * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE} * @see #isExpired */ @Override public Element get(Serializable key) throws IllegalStateException, CacheException { return this.get((Object) key); }
/** * Gets an element from the cache. Updates Element Statistics * <p/> * Note that the Element's lastAccessTime is always the time of this get. * Use {@link #getQuiet(Object)} to peak into the Element to see its last access time with get * * @param key a serializable value * @return the element, or null, if it does not exist. * @throws IllegalStateException if the cache is not {@link net.sf.ehcache.Status#STATUS_ALIVE} * @see #isExpired */ @Override public Element get(Serializable key) throws IllegalStateException, CacheException { return this.get((Object) key); }
@Override public Object get(Object key) { final Element element = ehcache.get(key); try { return element != null ? element.getObjectValue() : null; } catch (LockTimeoutException e) { log.error("Detected 1 thread stuck in generating response for {}. This might be temporary if obtaining the response is resource intensive or when accessing remote resources.", key); throw e; } }
/** * @param key The key for the cached resource * @return The cached resource for the specified key. */ protected final CachedResource getResourceFromCache(String key) { Element element = this._cache.get(key); if (element == null || element.getObjectValue() == null) return null; return (CachedResource)element.getObjectValue(); }
@Override public boolean hasElement(Object key) { // we can't use isKeyInCache(), as it does not check for the element's expiration // which may lead to unexpected results. // return ehcache.isKeyInCache(key); try { // get() and getQuiet() do check for expiration and return null if the element was expired. // we can't use getQuiet, as it's non-blocking which could lead to multiple copies of same page to be generated // if page is requested while previous request for same page is still being processed by different thread return ehcache.get(key) != null; } catch (LockTimeoutException e) { log.error("Detected 1 thread stuck in generating response for {}. This might be temporary if obtaining the response is resource intensive or when accessing remote resources.", key); // FYI: in case you want to return some value instead of re-throwing exception: this is a dilemma ... obviously resource does not exist yet, but being stuck here for while means that it is either being generated or it takes time to generate. // returning false would mean server attempts to generate the response again, possibly loosing another thread in the process // returning true means server will assume resource exists and will try to retrieve it later, possibly failing with the same error throw e; } }
@Override public Element get(final Object key) { if (key == null) { throw new NullPointerException("null keys are not permitted"); } Element element = super.get(key); return (element != null) ? element : loadValueAndReleaseLock(key); }
/** * {@inheritDoc} */ public StoreEntry get(Key key) { Element entry = null; try { /* This may block. */ entry = cache.get(key); } catch (LockTimeoutException e) { throw new RuntimeException(); } catch (RuntimeException e) { /* Release the lock that may have been acquired. */ cache.put(new Element(key, null)); } StoreEntry result = null; if (entry != null) { /* * We don't need to check isExpired() on the result, since ehcache takes care of expiring entries for us. * c.f. the get(Key) implementation in this class. */ result = (StoreEntry) entry.getObjectValue(); } return result; }
Element element = cache.get( key ); if ( element != null )
/** * Looks up an entry. creating it if not found. */ @Override public Element get(final Object key) throws LockTimeoutException { Element element = super.get(key); if (element == null) { try { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired element = new Element(key, null); throw new CacheException("Could not fetch object for cache entry with key \"" + key + "\".", throwable); } finally { put(element); } } return element; }
/** * Looks up an entry. creating it if not found. */ @Override public Element get(final Object key) throws LockTimeoutException { Element element = super.get(key); if (element == null) { try { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired element = new Element(key, null); throw new CacheException("Could not fetch object for cache entry with key \"" + key + "\".", throwable); } finally { put(element); } } return element; }
/** * Looks up an entry. creating it if not found. */ @Override public Element get(final Object key) throws LockTimeoutException { Element element = super.get(key); if (element == null) { try { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired element = new Element(key, null); throw new CacheException("Could not fetch object for cache entry with key \"" + key + "\".", throwable); } finally { put(element); } } return element; }