FileDocCategorySizeDatePackage
EhCache.javaAPI DocHibernate 3.2.56927Fri Nov 03 12:05:12 GMT 2006org.hibernate.cache

EhCache

public class EhCache extends Object implements Cache
EHCache plugin for Hibernate

EHCache uses a {@link net.sf.ehcache.store.MemoryStore} and a {@link net.sf.ehcache.store.DiskStore}. The {@link net.sf.ehcache.store.DiskStore} requires that both keys and values be {@link java.io.Serializable}. However the MemoryStore does not and in ehcache-1.2 nonSerializable Objects are permitted. They are discarded if an attempt it made to overflow them to Disk or to replicate them to remote cache peers.

author
Greg Luck
author
Emmanuel Bernard

Fields Summary
private static final Log
log
private static final int
SIXTY_THOUSAND_MS
private net.sf.ehcache.Cache
cache
Constructors Summary
public EhCache(net.sf.ehcache.Cache cache)
Creates a new Hibernate pluggable cache based on a cache name.

param
cache The underlying EhCache instance to use.


	                    	 
	   
		this.cache = cache;
	
Methods Summary
public voidclear()
Remove all elements in the cache, but leave the cache in a useable state.

throws
CacheException

		try {
			cache.removeAll();
		}
		catch (IllegalStateException e) {
			throw new CacheException( e );
		}
		catch (net.sf.ehcache.CacheException e) {
			throw new CacheException( e );
		}
	
public voiddestroy()
Remove the cache and make it unuseable.

throws
CacheException

		try {
			cache.getCacheManager().removeCache( cache.getName() );
		}
		catch (IllegalStateException e) {
			throw new CacheException( e );
		}
		catch (net.sf.ehcache.CacheException e) {
			throw new CacheException( e );
		}
	
public java.lang.Objectget(java.lang.Object key)
Gets a value of an element which matches the given key.

param
key the key of the element to return.
return
The value placed into the cache with an earlier put, or null if not found or expired
throws
CacheException

		try {
			if ( log.isDebugEnabled() ) {
				log.debug( "key: " + key );
			}
			if ( key == null ) {
				return null;
			}
			else {
				Element element = cache.get( key );
				if ( element == null ) {
					if ( log.isDebugEnabled() ) {
						log.debug( "Element for " + key + " is null" );
					}
					return null;
				}
				else {
					return element.getObjectValue();
				}
			}
		}
		catch (net.sf.ehcache.CacheException e) {
			throw new CacheException( e );
		}
	
public longgetElementCountInMemory()

		try {
			return cache.getMemoryStoreSize();
		}
		catch (net.sf.ehcache.CacheException ce) {
			throw new CacheException( ce );
		}
	
public longgetElementCountOnDisk()

		return cache.getDiskStoreSize();
	
public java.lang.StringgetRegionName()

		return cache.getName();
	
public longgetSizeInMemory()
Warning: This method can be very expensive to run. Allow approximately 1 second per 1MB of entries. Running this method could create liveness problems because the object lock is held for a long period

return
the approximate size of memory ehcache is using for the MemoryStore for this cache

		try {
			return cache.calculateInMemorySize();
		}
		catch (Throwable t) {
			return -1;
		}
	
public intgetTimeout()
Returns the lock timeout for this cache.

		// 60 second lock timeout
		return Timestamper.ONE_MS * SIXTY_THOUSAND_MS;
	
public voidlock(java.lang.Object key)
Calls to this method should perform there own synchronization. It is provided for distributed caches. Because EHCache is not distributed this method does nothing.

	
public longnextTimestamp()
Gets the next timestamp;

		return Timestamper.next();
	
public voidput(java.lang.Object key, java.lang.Object value)
Puts an object into the cache.

param
key a key
param
value a value
throws
CacheException if the {@link CacheManager} is shutdown or another {@link Exception} occurs.

		try {
			Element element = new Element( key, value );
			cache.put( element );
		}
		catch (IllegalArgumentException e) {
			throw new CacheException( e );
		}
		catch (IllegalStateException e) {
			throw new CacheException( e );
		}
		catch (net.sf.ehcache.CacheException e) {
			throw new CacheException( e );
		}

	
public java.lang.Objectread(java.lang.Object key)

		return get( key );
	
public voidremove(java.lang.Object key)
Removes the element which matches the key.

If no element matches, nothing is removed and no Exception is thrown.

param
key the key of the element to remove
throws
CacheException

		try {
			cache.remove( key );
		}
		catch (ClassCastException e) {
			throw new CacheException( e );
		}
		catch (IllegalStateException e) {
			throw new CacheException( e );
		}
		catch (net.sf.ehcache.CacheException e) {
			throw new CacheException( e );
		}
	
public java.util.MaptoMap()

		try {
			Map result = new HashMap();
			Iterator iter = cache.getKeys().iterator();
			while ( iter.hasNext() ) {
				Object key = iter.next();
				result.put( key, cache.get( key ).getObjectValue() );
			}
			return result;
		}
		catch (Exception e) {
			throw new CacheException( e );
		}
	
public java.lang.StringtoString()

		return "EHCache(" + getRegionName() + ')";
	
public voidunlock(java.lang.Object key)
Calls to this method should perform there own synchronization. It is provided for distributed caches. Because EHCache is not distributed this method does nothing.

	
public voidupdate(java.lang.Object key, java.lang.Object value)
Puts an object into the cache.

param
key a key
param
value a value
throws
CacheException if the {@link CacheManager} is shutdown or another {@link Exception} occurs.

		put( key, value );