net.sf.ehcache.constructs.blocking

Class SelfPopulatingCache

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 SelfPopulatingCache 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 191 2006-09-03 22:41:48Z gregluck $

Author: Greg Luck

Field Summary
protected CacheEntryFactoryfactory
A factory for creating entries, given a key
Constructor Summary
SelfPopulatingCache(Ehcache cache, CacheEntryFactory factory)
Creates a SelfPopulatingCache.
Method Summary
Elementget(Object key)
Looks up an entry. creating it if not found.
voidrefresh()
Refresh the elements of this cache.
protected voidrefreshElement(Element element, Ehcache backingCache)
Refresh a single element.
protected voidsetThreadName(String method, Object key)
Rename the thread for easier thread dump reading.

Field Detail

factory

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

Constructor Detail

SelfPopulatingCache

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

Method Detail

get

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

refresh

public void refresh()
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.

Threads entering this method are temporarily renamed, so that a Thread Dump will show meaningful information.

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

refreshElement

protected void refreshElement(Element element, Ehcache backingCache)
Refresh a single element.

Parameters: element the Element to refresh backingCache the underlying Ehcache.

Throws: Exception

setThreadName

protected void setThreadName(String method, Object key)
Rename the thread for easier thread dump reading.

Parameters: method the method about to be called key the key being operated on