HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > Using Tangosol Coherence Cache

Using Tangosol Coherence Cache

Hibernate 2.1 features support for plugin cache providers and is designed to integrate with distributed caches. (2.1 also implements more aggressive use of the cache.) net.sf.hibernate.cache.CacheProvider is the extension point for user-defined cache integration.

The following classes implement integration with Tangosol Coherence replicated cache. (Coherence is a commercial product, so we can't include this code in Hibernate itself.)

To use this code, you need to download Hibernate 2.1 beta 2.

package net.sf.hibernate.cache;

import java.util.Properties;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

/**
 * Tangosol Coherence plugin for Hibernate.
 * Use <tt>hibernate.cache.provider_class=net.sf.hibernate.cache.CoherenceCacheProvider</tt>
 * in Hibernate 2.1 beta 2 or later.
 * 
 * @author Gavin King
 */
public class CoherenceCacheProvider implements CacheProvider {

    public Cache buildCache(String regionName, Properties properties)
        throws CacheException {
        NamedCache cache = CacheFactory.getCache(regionName);
        return new CoherenceCache(cache);
    }

    public long nextTimestamp() {
        return CacheFactory.getCluster().getTimeMillis();
    }

}
package net.sf.hibernate.cache;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

/**
 * Tangosol Coherence plugin for Hibernate.
 * 
 * @author Gavin King
 */
public class CoherenceCache implements Cache {
    
    private NamedCache cache;
    
    public CoherenceCache(NamedCache cache) {
        this.cache = cache;
    }

    public Object get(Object key) throws CacheException {
        return cache.get(key);
    }

    public void put(Object key, Object value) throws CacheException {
        cache.put(key, value);
    }

    public void remove(Object key) throws CacheException {
        cache.remove(key);

    }

    public void clear() throws CacheException {
        cache.clear();
    }

    public void destroy() throws CacheException {
        cache.destroy();
    }

    public long nextTimestamp() {
        return CacheFactory.getCluster().getTimeMillis();
    }

    public void lock(Object key) throws CacheException {
        cache.lock(key);
    }

    public void unlock(Object key) throws CacheException {
        cache.unlock(key);
    }

    public int getTimeout() {
        return 10000; //not sure about this!
    }

}
      

coWiki