Memcached 作为Hibernate的二级缓存

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.iteye.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.iteye.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();


}

}


上述思路和代码希望能够起到抛砖引玉作用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值