ehcache

net.sf.ehcache.constructs.blocking
Class UpdatingSelfPopulatingCache

java.lang.Object
  extended by net.sf.ehcache.constructs.blocking.BlockingCache
      extended by net.sf.ehcache.constructs.blocking.SelfPopulatingCache
          extended by net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache
All Implemented Interfaces:
java.lang.Cloneable, Ehcache

public class UpdatingSelfPopulatingCache
extends SelfPopulatingCache

A Cache backed cache that creates entries on demand.

Clients of the cache simply call it without needing knowledge of whether the entry exists in the cache, or whether it needs updating before use.

Thread safety depends on the factory being used. The UpdatingCacheEntryFactory should be made thread safe. In addition users of returned values should not modify their contents.

Version:
$Id: UpdatingSelfPopulatingCache.java 575 2008-01-30 07:22:04Z gregluck $
Author:
Greg Luck

Field Summary
 
Fields inherited from class net.sf.ehcache.constructs.blocking.SelfPopulatingCache
factory
 
Fields inherited from class net.sf.ehcache.constructs.blocking.BlockingCache
cache, LOCK_NUMBER, locks, timeoutMillis
 
Constructor Summary
UpdatingSelfPopulatingCache(Ehcache cache, UpdatingCacheEntryFactory factory)
          Creates a SelfPopulatingCache.
 
Method Summary
 Element get(java.lang.Object key)
          Looks up an object.
 void refresh()
          This method should not be used.
protected  void update(java.lang.Object key)
          Element can never be null.
 
Methods inherited from class net.sf.ehcache.constructs.blocking.SelfPopulatingCache
refreshElement
 
Methods inherited from class net.sf.ehcache.constructs.blocking.BlockingCache
bootstrap, calculateInMemorySize, clearStatistics, clone, dispose, evictExpiredElements, flush, get, getAllWithLoader, getAverageGetTime, getBootstrapCacheLoader, getCache, getCacheConfiguration, getCacheEventNotificationService, getCacheExceptionHandler, getCacheLoader, getCacheManager, getDiskExpiryThreadIntervalSeconds, getDiskStoreSize, getGuid, getKeys, getKeysNoDuplicateCheck, getKeysWithExpiryCheck, getLockForKey, getMaxElementsInMemory, getMaxElementsOnDisk, getMemoryStoreEvictionPolicy, getMemoryStoreSize, getName, getQuiet, getQuiet, getSize, getStatistics, getStatisticsAccuracy, getStatus, getTimeoutMillis, getTimeToIdleSeconds, getTimeToLiveSeconds, getWithLoader, initialise, isDisabled, isDiskPersistent, isElementInMemory, isElementInMemory, isElementOnDisk, isElementOnDisk, isEternal, isExpired, isKeyInCache, isOverflowToDisk, isValueInCache, liveness, load, loadAll, put, put, putQuiet, registerCacheExtension, remove, remove, remove, remove, removeAll, removeAll, removeQuiet, removeQuiet, setBootstrapCacheLoader, setCacheExceptionHandler, setCacheLoader, setCacheManager, setDisabled, setDiskStorePath, setName, setStatisticsAccuracy, setTimeoutMillis, unregisterCacheExtension
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.ehcache.Ehcache
toString
 

Constructor Detail

UpdatingSelfPopulatingCache

public UpdatingSelfPopulatingCache(Ehcache cache,
                                   UpdatingCacheEntryFactory factory)
                            throws CacheException
Creates a SelfPopulatingCache.

Throws:
CacheException
Method Detail

get

public Element get(java.lang.Object key)
            throws LockTimeoutException
Looks up an object.

If null, it creates it. If not null, it updates it. For performance this method should only be used with UpdatingCacheEntryFactory's

It is expected that gets, which update as part of the get, might take considerable time. Access to the cache cannot be blocked while that is happening. This method is therefore not synchronized. Mutexes are used for thread safety based on key

Specified by:
get in interface Ehcache
Overrides:
get in class SelfPopulatingCache
Parameters:
key -
Returns:
a value
Throws:
CacheException
LockTimeoutException - if timeout millis is non zero and this method has been unable to acquire a lock in that time
See Also:
Ehcache.isExpired(net.sf.ehcache.Element)

update

protected void update(java.lang.Object key)
Element can never be null. Add a null guard just in case.

Parameters:
key -

refresh

public void refresh()
             throws CacheException
This method should not be used. Because elements are always updated before they are returned, it makes no sense to refresh this cache.

Overrides:
refresh in class SelfPopulatingCache
Throws:
CacheException

ehcache