现在的位置: 首页 > 综合 > 正文

Memcached 作为Hibernate的二级缓存

2013年12月05日 ⁄ 综合 ⁄ 共 4361字 ⁄ 字号 评论关闭
 hibernate的缓存机制是其成为主流持久层霸主地位的重要组成部分。二级缓存是SessionFactory级别的全局缓存,可以通过配置文件的hibernate.cache.provider_class 进行指定。
目前Hiberante 的二级缓存实现方式:

1.EhCache
2,Hashtable
3.JndiBoundTreeCache
4.NoCache
5.OptimisticTreeCache
6.OSCache
7.TreeCache
8.SwarmCache

大致分为上述8种缓存方案的实现,主要分为内存和文件两种方式进行存储。

hibernate的二级缓存默认是不开启的,就是采用NoCacheProvider

public static final String DEF_CACHE_PROVIDER = NoCacheProvider.class.getName();

就提出较大的设想,采用Memcached作为hibernate的二级缓存存储方式。采用Memcached作为Hibernate的二级缓存具有良好的分布式特性。

多台基于Hiberante进行不部署部署,可以共享二级缓存,是不是很爽,赫赫!

在这里我们就采用Memcached作为hibernate的二级缓存。主要实现Hibernate的两个接口: org.hibernate.cache.Cache,org.hibernate.cache.CacheProvider,实现我们自己的方法。在这里,我们采用

如下代码只给出了关键思路和主要方法,一些异常,日志,属性文件操作等非关键代码没有加,算是一个Demo code吧。
下面定义:MemcachedCache ,

package xxxx.memcached;

import java.util.Map;
import java.util.Properties;

import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/*
 * Copyright By C.F @http://masterkey.javaeye.com
 * hibernate with Memcached Cache
 * Memcached 1.2.x ,java_memcached from http://www.whalin.com/memcached/
 * demo code 
 */
public class MemCachedCache implements Cache {
	private MemCachedClient cacheClient;
	private String _regionName;
	public MemCachedCache(String regionName, Properties properties)
	{
		_regionName = regionName;
		//serverlist = cache1.int.meetup.com:12345;cache0.int.meetup.com:12345
		String servers =(String)(properties.get("serverlist"));
		String[] serverlist = servers.split(";"); 
		// initialize the pool for memcache servers
		SockIOPool pool = SockIOPool.getInstance();

		pool.setServers( serverlist );
		
		pool.setInitConn(Integer.parseInt((String)properties.get("InitConn")));
		pool.setMinConn(Integer.parseInt((String)properties.get("MinConn")));
		pool.setMaxConn(Integer.parseInt((String)properties.get("MaxConn")));
		pool.setMaintSleep(Integer.parseInt((String)properties.get("MaintSleep")));

		pool.setNagle(Boolean.parseBoolean((String)properties.get("Nagle")));
		pool.initialize();	


		
	}
	public void clear() throws CacheException {
		// TODO Auto-generated method stub
		
	}

	public void destroy() throws CacheException {
		// TODO Auto-generated method stub
		
	}

	public Object get(Object key) throws CacheException {
		// TODO Auto-generated method stub

		return cacheClient.get(toString(key));
	}

	public long getElementCountInMemory() {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getElementCountOnDisk() {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getRegionName() {
		// TODO Auto-generated method stub
		return _regionName;
	}

	public long getSizeInMemory() {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getTimeout() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void lock(Object key) throws CacheException {
		// TODO Auto-generated method stub
		
	}

	public long nextTimestamp() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void put(Object key, Object value) throws CacheException {
		// TODO Auto-generated method stub
		cacheClient.set(toString(key), value);
		
	}

	public Object read(Object key) throws CacheException {
		// TODO Auto-generated method stub
		return get(key);
	}

	public void remove(Object key) throws CacheException {
		// TODO Auto-generated method stub
		cacheClient.delete(toString(key));
		
	}

	public Map toMap() {
		// TODO Auto-generated method stub
		return null;
	}

	public void unlock(Object key) throws CacheException {
		// TODO Auto-generated method stub
		
	}

	public void update(Object key, Object value) throws CacheException {
		// TODO Auto-generated method stub
		cacheClient.replace(toString(key), value);
	}
	public String toString(Object key)
	{
		return _regionName+(String)key;
	}


}

接着我们需要实现MemcachedCacheProvider

package xxxx.memcached;

import java.util.Properties;

import org.hibernate.cache.Cache;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.CacheProvider;
import org.hibernate.cache.Timestamper;

import com.danga.MemCached.SockIOPool;

/*
 * Copyright By C.F @http://masterkey.javaeye.com
 * hibernate with Memcached Cache
 * Memcached 1.2.x ,java_memcached from http://www.whalin.com/memcached/
 * demo code 
 */
public class MemCachedProvider implements CacheProvider{
    private SockIOPool pool;
    private MemCachedCache memcachedCache = null;
	public Cache buildCache(String regionName, Properties properties)
			throws CacheException {
		// TODO Auto-generated method stub

		return new MemCachedCache(regionName,properties);
	}

	public boolean isMinimalPutsEnabledByDefault() {
		// TODO Auto-generated method stub
		return false;
	}

	public long nextTimestamp() {
		// TODO Auto-generated method stub
		return Timestamper.next();
	}

	public void start(Properties properties) throws CacheException {
		// TODO Auto-generated method stub
		
	}

	public void stop() {
		// TODO Auto-generated method stub
        if(null != pool)
            pool.shutDown();


    }

}

上述思路和代码希望能够起到抛砖引玉作用。

抱歉!评论已关闭.