Top Description Fields Constructors Methods
net.sf.ehcache.constructs.blocking

public Class SelfPopulatingCache

extends BlockingCache
Class Inheritance
Known Direct Subclasses
net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache
Imports
net.sf.ehcache.CacheException, .Ehcache, .Element, org.slf4j.Logger, .LoggerFactory, java.util.Collection, .Iterator

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 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.

Author
Greg Luck
Version
$Id: SelfPopulatingCache.java 10789 2018-04-26 02:08:13Z adahanne $

Field Summary

Modifier and TypeField and Description
protected final CacheEntryFactory
factory

A factory for creating entries, given a key

private static final Logger
Inherited from net.sf.ehcache.constructs.blocking.BlockingCache:
timeoutMillis

Constructor Summary

AccessConstructor and Description
public
SelfPopulatingCache(final Ehcache cache, final CacheEntryFactory factory)

Creates a SelfPopulatingCache.

public
SelfPopulatingCache(Ehcache cache, int numberOfStripes, CacheEntryFactory factory)

Create a SelfPopulatingCache, with a specific number of stripes passed to the underlying net.sf.ehcache.constructs.blocking.BlockingCache.

Method Summary

Modifier and TypeMethod and Description
public Element
get(final Object
an Object value
key
)

Overrides net.sf.ehcache.constructs.blocking.BlockingCache.get.

Implements net.sf.ehcache.Ehcache.get.

Looks up an entry.
protected static Element

Returns:

the Element to be put back in the cache
makeAndCheckElement
(Object key, Object value)

Both CacheEntryFactory can return an Element rather than just a regular value this method test this, making a fresh Element otherwise.

public void
refresh()

Refresh the elements of this cache.

public void
refresh(boolean
whether the backing cache is quietly updated or not, if true replication will not occur
quiet
)

Refresh the elements of this cache.

public Element

Returns:

the refreshed Element
refresh
(Object key)

Refresh a single element.

public Element

Returns:

the refreshed Element
refresh
(Object key, boolean
whether the backing cache is quietly updated or not, if true replication will not occur
quiet
)

Refresh a single element.

protected void
refreshElement(final Element
the Element to refresh
element
,
Ehcache
the underlying Ehcache.
backingCache
)

Refresh a single element.

protected Element

Returns:

the refreshed Element
refreshElement
(final Element
the Element to refresh
element
,
Ehcache
the underlying Ehcache.
backingCache
,
boolean
whether to use putQuiet or not, if true replication will not occur
quiet
)

Refresh a single element.

Inherited from net.sf.ehcache.constructs.blocking.BlockingCache:
getgetAllWithLoadergetCachegetLockForKeygetTimeoutMillisgetWithLoaderlivenessloadloadAllputputputIfAbsentputIfAbsentputQuietputWithWriterregisterCacheLoadersetTimeoutMillisunregisterCacheLoader

Field Detail

factoryback to summary
protected final CacheEntryFactory factory

A factory for creating entries, given a key

LOGback to summary
private static final Logger LOG

Constructor Detail

SelfPopulatingCacheback to summary
public SelfPopulatingCache(final Ehcache cache, final CacheEntryFactory factory) throws CacheException

Creates a SelfPopulatingCache.

SelfPopulatingCacheback to summary
public SelfPopulatingCache(Ehcache cache, int numberOfStripes, CacheEntryFactory factory) throws CacheException

Create a SelfPopulatingCache, with a specific number of stripes passed to the underlying net.sf.ehcache.constructs.blocking.BlockingCache.

Method Detail

getback to summary
public Element get(final Object key) throws LockTimeoutException

Overrides net.sf.ehcache.constructs.blocking.BlockingCache.get.

Implements net.sf.ehcache.Ehcache.get.

Looks up an entry. creating it if not found.

Parameters
key:Object

Doc from net.sf.ehcache.Ehcache.get.

an Object value

Returns:Element

Doc from net.sf.ehcache.Ehcache.get.

the element, or null, if it does not exist.

Annotations
@Override
Exceptions
LockTimeoutException:

Doc from net.sf.ehcache.constructs.blocking.BlockingCache.get.

if timeout millis is non zero and this method has been unable to acquire a lock in that time

makeAndCheckElementback to summary
protected static Element makeAndCheckElement(Object key, Object value) throws CacheException

Both CacheEntryFactory can return an Element rather than just a regular value this method test this, making a fresh Element otherwise. It also enforces the rule that the CacheEntryFactory must provide the same key (via equals() not necessarily same object) if it is returning an Element.

Returns:Element

the Element to be put back in the cache

Exceptions
CacheException:
for various illegal states which could be harmful
refreshback to summary
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. Note that the refreshed elements will not be replicated to any cache peers.

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

  • use timeToIdle to discard elements unused for a period of time
  • use timeToLive to discard elmeents that have existed beyond their allotted lifespan
refreshback to summary
public void refresh(boolean quiet) 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 if argument 0 is true, so that statistics are not affected, but note that replication will then not occur

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

  • use timeToIdle to discard elements unused for a period of time
  • use timeToLive to discard elmeents that have existed beyond their allotted lifespan
Parameters
quiet:boolean

whether the backing cache is quietly updated or not, if true replication will not occur

Since
1.6.1
refreshback to summary
public Element refresh(Object key) throws CacheException

Refresh a single element.

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.

If the element is absent it is created

Quiet methods are used, so that statistics are not affected. Note that the refreshed element will not be replicated to any cache peers.

Returns:Element

the refreshed Element

Since
1.6.1
refreshback to summary
public Element refresh(Object key, boolean quiet) throws CacheException

Refresh a single element.

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.

If the element is absent it is created

Quiet methods are used if argument 1 is true, so that statistics are not affected, but note that replication will then not occur

Parameters
quiet:boolean

whether the backing cache is quietly updated or not, if true replication will not occur

Returns:Element

the refreshed Element

Since
1.6.1
refreshElementback to summary
protected void refreshElement(final Element element, Ehcache backingCache) throws Exception

Refresh a single element.

Parameters
element:Element

the Element to refresh

backingCache:Ehcache

the underlying Ehcache.

refreshElementback to summary
protected Element refreshElement(final Element element, Ehcache backingCache, boolean quiet) throws Exception

Refresh a single element.

Parameters
element:Element

the Element to refresh

backingCache:Ehcache

the underlying Ehcache.

quiet:boolean

whether to use putQuiet or not, if true replication will not occur

Returns:Element

the refreshed Element

Since
1.6.1