/** * Decorate the given Cache, if necessary. * @param cache the raw Cache object, based on the configuration of this FactoryBean * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Ehcache cache) { if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); } else { return new SelfPopulatingCache(cache, this.cacheEntryFactory); } } if (this.blocking) { return new BlockingCache(cache); } return cache; }
/** * 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); }
/** * Synchronized version of getName to test liveness of the object lock. * <p> * The time taken for this method to return is a useful measure of runtime contention on the cache. * * @return the name of the cache. */ public synchronized String liveness() { return getName(); }
@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; }
/** * Replace the cache with a BlockingCache decorated one if this has not been done yet. * The cacheLoaderTimeoutMillis of the original cache will be used as the timeoutMillis of the BlockingCache. * @param ehcache the original cache * @return a BlockingCache wrapping the original one */ protected BlockingCache replaceWithBlockingCacheIfNot(Ehcache ehcache){ if (ehcache instanceof BlockingCache){ return (BlockingCache) ehcache; } BlockingCache blockingCache = new BlockingCache(ehcache); blockingCache.setTimeoutMillis((int)ehcache.getCacheConfiguration().getCacheLoaderTimeoutMillis()); cacheManager.replaceCacheWithDecoratedCache(ehcache, blockingCache); return blockingCache; }
try { checkNoReentry(request); Element element = blockingCache.get(key); if (element == null || element.getObjectValue() == null) { try { if (LOG.isDebugEnabled()) { LOG.debug("PageInfo ok. Adding to cache " + blockingCache.getName() + " with key " + key); blockingCache.put(new Element(key, pageInfo)); } else { if (LOG.isDebugEnabled()) { LOG.debug("PageInfo was not ok(200). Putting null into cache " + blockingCache.getName() + " with key " + key); blockingCache.put(new Element(key, null)); blockingCache.put(new Element(key, null)); throw new Exception(throwable);
@Override public void putWithWriter(final Element element) throws IllegalArgumentException, IllegalStateException, CacheException { doAndReleaseWriteLock(new PutAction<Void>(element) { @Override public Void put() { underlyingCache.putWithWriter(element); return null; } }); }
public Element get(final Object key) throws RuntimeException, LockTimeoutException { getObserver.begin(); Sync lock = getLockForKey(key); acquiredLockForKey(key, lock, LockType.READ); Element element; try { acquiredLockForKey(key, lock, LockType.WRITE); element = underlyingCache.get(key); if (element != null) {
@Override public StoreEntry create(Long key, int timeToLive) { StoreEntryImpl result = new StoreEntryImpl(timeToLive); Element element = new Element(key, result); element.setTimeToLive(timeToLive); cache.put(element); return result; }
@Override protected Ehcache decorateCache(Ehcache cache) { Ehcache decorated = super.decorateCache(cache); if (decorated instanceof BlockingCache) { ((BlockingCache) decorated).setTimeoutMillis(timeoutMillis); } return decorated; } }
private CacheLockProvider getCacheLockProvider() { CacheLockProvider provider = cacheLockProviderReference.get(); while (provider == null) { cacheLockProviderReference.compareAndSet(null, createCacheLockProvider()); provider = cacheLockProviderReference.get(); } return provider; }
protected void createCache(String name) { synchronized (this.getClass()) { cacheManager.addCache(name); final Ehcache cache = cacheManager.getEhcache(name); // default EHCache is non blocking. Since our impl always requires blocking cache wrap it here. final BlockingCache newBlockingCache = new BlockingCache(cache); newBlockingCache.setTimeoutMillis(getBlockingTimeout()); cacheManager.replaceCacheWithDecoratedCache(cache, newBlockingCache); } }
/** * {@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; }
@Override public Element putIfAbsent(final Element element) throws NullPointerException { return doAndReleaseWriteLock(new PutAction<Element>(element) { @Override public Element put() { return underlyingCache.putIfAbsent(element); } }); }
public Element get(final Object key) throws RuntimeException, LockTimeoutException { Sync lock = getLockForKey(key); acquiredLockForKey(key, lock, LockType.READ); Element element; try { acquiredLockForKey(key, lock, LockType.WRITE); element = underlyingCache.getQuiet(key); if (element != null) {
@Override public void put(Object key, Object value) { final Element element = new Element(key, value); ehcache.put(element); }
private CacheLockProvider getCacheLockProvider() { CacheLockProvider provider = cacheLockProviderReference.get(); while (provider == null) { cacheLockProviderReference.compareAndSet(null, createCacheLockProvider()); provider = cacheLockProviderReference.get(); } return provider; }
/** * Create and return a reference to {@link Cache} object. * * @return instance of {@link Cache}. */ public static Cache createInstance(String name, net.sf.ehcache.CacheManager cacheManager) { net.sf.ehcache.Ehcache cache = cacheManager.getEhcache(name); if(cache == null) { String error = "createInstance cache: " + name + " is null"; throw new CfgRuntimeException(GlobalErrIds.FT_CACHE_NOT_CONFIGURED, error); } BlockingCache blockingCache = new BlockingCache(cache); blockingCache.setTimeoutMillis(60000); cacheManager.replaceCacheWithDecoratedCache(cache, blockingCache); return new EhCacheImpl(name, blockingCache); } }
/** * Decorate the given Cache, if necessary. * @param cache the raw Cache object, based on the configuration of this FactoryBean * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Ehcache cache) { if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); } else { return new SelfPopulatingCache(cache, this.cacheEntryFactory); } } if (this.blocking) { return new BlockingCache(cache); } return cache; }
/** * 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; }