ehcache

net.sf.ehcache.hibernate
Class EhCache

java.lang.Object
  extended by net.sf.ehcache.hibernate.EhCache
All Implemented Interfaces:
org.hibernate.cache.Cache

public final class EhCache
extends java.lang.Object
implements org.hibernate.cache.Cache

EHCache plugin for Hibernate.

EHCache uses a MemoryStore and a DiskStore.

The DiskStore requires that both keys and values be 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 612 2008-05-07 22:48:26Z gregluck $
Author:
Greg Luck, Emmanuel Bernard

Constructor Summary
EhCache(Ehcache cache)
          Creates a new Hibernate pluggable cache by name.
 
Method Summary
 void clear()
          Remove all elements in the cache, but leave the cache in a useable state.
 void destroy()
          Remove the cache and make it unuseable.
 java.lang.Object get(java.lang.Object key)
          Gets a value of an element which matches the given key.
(package private)  Ehcache getBackingCache()
          Package protected method used for testing
 long getElementCountInMemory()
           
 long getElementCountOnDisk()
           
 java.lang.String getRegionName()
           
 long getSizeInMemory()
          Warning: This method can be very expensive to run.
 int getTimeout()
          Returns the lock timeout for this cache, which is 60s
 void lock(java.lang.Object key)
          Calls to this method should perform their own synchronization.
 long nextTimestamp()
          Gets the next timestamp;
 void put(java.lang.Object key, java.lang.Object value)
          Puts an object into the cache.
 java.lang.Object read(java.lang.Object key)
          Gets an object from the cache.
 void remove(java.lang.Object key)
          Removes the element which matches the key.
 java.util.Map toMap()
           
 java.lang.String toString()
           
 void unlock(java.lang.Object key)
          Calls to this method should perform their own synchronization.
 void update(java.lang.Object key, java.lang.Object value)
          Updates an object in the cache, or if it does not exist, inserts it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EhCache

public EhCache(Ehcache 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

get

public final java.lang.Object get(java.lang.Object key)
                           throws org.hibernate.cache.CacheException
Gets a value of an element which matches the given key.

Specified by:
get in interface org.hibernate.cache.Cache
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

read

public final java.lang.Object read(java.lang.Object key)
                            throws org.hibernate.cache.CacheException
Gets an object from the cache.

Specified by:
read in interface org.hibernate.cache.Cache
Parameters:
key - an Object value
Returns:
the Object, or null if not found
Throws:
org.hibernate.cache.CacheException

update

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

Specified by:
update in interface org.hibernate.cache.Cache
Parameters:
key - an Object key
value - an Object value
Throws:
org.hibernate.cache.CacheException - if the CacheManager is shutdown or another Exception occurs.

put

public final void put(java.lang.Object key,
                      java.lang.Object value)
               throws org.hibernate.cache.CacheException
Puts an object into the cache.

Specified by:
put in interface org.hibernate.cache.Cache
Parameters:
key - an Object key
value - an Object value
Throws:
org.hibernate.cache.CacheException - if the CacheManager is shutdown or another Exception occurs.

remove

public final void remove(java.lang.Object key)
                  throws org.hibernate.cache.CacheException
Removes the element which matches the key.

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

Specified by:
remove in interface org.hibernate.cache.Cache
Parameters:
key - the key of the element to remove
Throws:
org.hibernate.cache.CacheException

clear

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

Specified by:
clear in interface org.hibernate.cache.Cache
Throws:
org.hibernate.cache.CacheException

destroy

public final void destroy()
                   throws org.hibernate.cache.CacheException
Remove the cache and make it unuseable.

Specified by:
destroy in interface org.hibernate.cache.Cache
Throws:
org.hibernate.cache.CacheException

lock

public final void lock(java.lang.Object key)
                throws org.hibernate.cache.CacheException
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.

Specified by:
lock in interface org.hibernate.cache.Cache
Throws:
org.hibernate.cache.CacheException

unlock

public final void unlock(java.lang.Object key)
                  throws org.hibernate.cache.CacheException
Calls to this method should perform their own synchronization.

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

Specified by:
unlock in interface org.hibernate.cache.Cache
Throws:
org.hibernate.cache.CacheException

nextTimestamp

public final long nextTimestamp()
Gets the next timestamp;

Specified by:
nextTimestamp in interface org.hibernate.cache.Cache

getTimeout

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

Specified by:
getTimeout in interface org.hibernate.cache.Cache

getRegionName

public final java.lang.String getRegionName()
Specified by:
getRegionName in interface org.hibernate.cache.Cache
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

Specified by:
getSizeInMemory in interface org.hibernate.cache.Cache
Returns:
the approximate size of memory ehcache is using for the MemoryStore for this cache

getElementCountInMemory

public final long getElementCountInMemory()
Specified by:
getElementCountInMemory in interface org.hibernate.cache.Cache
Returns:
the number of elements in ehcache's MemoryStore

getElementCountOnDisk

public final long getElementCountOnDisk()
Specified by:
getElementCountOnDisk in interface org.hibernate.cache.Cache
Returns:
the number of elements in ehcache's DiskStore. 0 is there is no DiskStore

toMap

public final java.util.Map toMap()
Specified by:
toMap in interface org.hibernate.cache.Cache
Returns:
a copy of the cache Elements as a Map

toString

public final java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
the region name, which is the cache name in ehcache

getBackingCache

final Ehcache getBackingCache()
Package protected method used for testing


ehcache