net.sf.ehcache.hibernate

Class EhCache

public final class EhCache extends Object implements Cache

EHCache plugin for Hibernate.

EHCache uses a MemoryStore and a DiskStore.

The DiskStore requires that both keys and values be 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.

Version: $Id: EhCache.java 53 2006-04-25 08:56:21Z gregluck $

Author: Greg Luck Emmanuel Bernard

Constructor Summary
EhCache(Cache cache)
Creates a new Hibernate pluggable cache by name.
Method Summary
voidclear()
Remove all elements in the cache, but leave the cache in a useable state.
voiddestroy()
Remove the cache and make it unuseable.
Objectget(Object key)
Gets a value of an element which matches the given key.
CachegetBackingCache()
Package protected method used for testing
longgetElementCountInMemory()
longgetElementCountOnDisk()
StringgetRegionName()
longgetSizeInMemory()
Warning: This method can be very expensive to run.
intgetTimeout()
Returns the lock timeout for this cache, which is 60s
voidlock(Object key)
Calls to this method should perform their own synchronization.
longnextTimestamp()
Gets the next timestamp;
voidput(Object key, Object value)
Puts an object into the cache.
Objectread(Object key)
Gets an object from the cache.
voidremove(Object key)
Removes the element which matches the key.
MaptoMap()
StringtoString()
voidunlock(Object key)
Calls to this method should perform their own synchronization.
voidupdate(Object key, Object value)
Updates an object in the cache, or if it does not exist, inserts it.

Constructor Detail

EhCache

public EhCache(Cache cache)
Creates a new Hibernate pluggable cache by name.

ehcache will look in ehcache.xml to load the configuration for the cache. If the cache is not there, it will use the defaultCache settings. It is always a good idea to specifically configure each cache.

Parameters: cache The backing ehcache cache.

Method Detail

clear

public final void clear()
Remove all elements in the cache, but leave the cache in a useable state.

Throws: CacheException

destroy

public final void destroy()
Remove the cache and make it unuseable.

Throws: CacheException

get

public final Object get(Object key)
Gets a value of an element which matches the given key.

Parameters: key the key of the element to return.

Returns: The value placed into the cache with an earlier put, or null if not found or expired

Throws: org.hibernate.cache.CacheException

getBackingCache

final Cache getBackingCache()
Package protected method used for testing

getElementCountInMemory

public final long getElementCountInMemory()

Returns: the number of elements in ehcache's MemoryStore

getElementCountOnDisk

public final long getElementCountOnDisk()

Returns: the number of elements in ehcache's DiskStore. 0 is there is no DiskStore

getRegionName

public final String getRegionName()

Returns: the region name of the cache, which is the cache name in ehcache

getSizeInMemory

public final long getSizeInMemory()
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

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

getTimeout

public final int getTimeout()
Returns the lock timeout for this cache, which is 60s

lock

public final void lock(Object key)
Calls to this method should perform their own synchronization. It is provided for distributed caches.

ehcache does not support distributed locking and therefore this method does nothing.

nextTimestamp

public final long nextTimestamp()
Gets the next timestamp;

put

public final void put(Object key, Object value)
Puts an object into the cache.

Parameters: key an Object key value an Object value

Throws: CacheException if the CacheManager is shutdown or another Exception occurs.

read

public final Object read(Object key)
Gets an object from the cache.

Parameters: key an Object value

Returns: the Object, or null if not found

Throws: CacheException

remove

public final void remove(Object key)
Removes the element which matches the key.

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

Parameters: key the key of the element to remove

Throws: CacheException

toMap

public final Map toMap()

Returns: a copy of the cache Elements as a Map

toString

public final String toString()

Returns: the region name, which is the cache name in ehcache

unlock

public final void unlock(Object key)
Calls to this method should perform their own synchronization.

ehcache does not support distributed locking and therefore this method does nothing.

update

public final void update(Object key, Object value)
Updates an object in the cache, or if it does not exist, inserts it.

Parameters: key an Object key value an Object value

Throws: CacheException if the CacheManager is shutdown or another Exception occurs.