ehcache

net.sf.ehcache.constructs.blocking
Class SelfPopulatingCache

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

public class SelfPopulatingCache
extends BlockingCache

A selfpopulating decorator for Ehcache that creates entries on demand.

Clients of the cache simply call it without needing knowledge of whether the entry exists in the cache.

The cache is designed to be refreshed. Refreshes operate on the backing cache, and do not degrade performance of BlockingCache.get(java.io.Serializable) calls.

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: SelfPopulatingCache.java 563 2007-11-23 22:07:38Z gregluck $
Author:
Greg Luck

Field Summary
protected  CacheEntryFactory factory
          A factory for creating entries, given a key
 
Fields inherited from class net.sf.ehcache.constructs.blocking.BlockingCache
cache, LOCK_NUMBER, locks, timeoutMillis
 
Constructor Summary
SelfPopulatingCache(Ehcache cache, CacheEntryFactory factory)
          Creates a SelfPopulatingCache.
 
Method Summary
 Element get(java.lang.Object key)
          Looks up an entry.
 void refresh()
          Refresh the elements of this cache.
protected  void refreshElement(Element element, Ehcache backingCache)
          Refresh a single element.
 
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
 

Field Detail

factory

protected final CacheEntryFactory factory
A factory for creating entries, given a key

Constructor Detail

SelfPopulatingCache

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

Throws:
CacheException
Method Detail

get

public Element get(java.lang.Object key)
            throws LockTimeoutException
Looks up an entry. creating it if not found.

Specified by:
get in interface Ehcache
Overrides:
get in class BlockingCache
Parameters:
key - an Object value
Returns:
the element, or null, if it does not exist.
Throws:
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)

refresh

public void refresh()
             throws CacheException
Refresh the elements of this cache.

Refreshes bypass the BlockingCache and act directly on the backing Ehcache. This way, BlockingCache gets can continue to return stale data while the refresh, which might be expensive, takes place.

Quiet methods are used, so that statistics are not affected.

Configure ehcache.xml to stop elements from being refreshed forever:

Throws:
CacheException

refreshElement

protected void refreshElement(Element element,
                              Ehcache backingCache)
                       throws java.lang.Exception
Refresh a single element.

Parameters:
element - the Element to refresh
backingCache - the underlying Ehcache.
Throws:
java.lang.Exception

ehcache