1、关于hibernate两级缓存介绍,这里分享两篇文章,讲得不错。
http://blog.csdn.net/defonds/article/details/2308972
http://blog.csdn.net/sd0902/article/details/8393750
2、后面我给予spring mvc 3.0写一个demo,展示如何使用ehcache缓存类配置使用,稍后贴出来
2.1 ehcache.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true" >
<!--
<cacheManagerPeerListenerFactory class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true"/>
-->
<defaultCache
maxElementsInMemory="0"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0">
</defaultCache>
<cache name="ehcache0"
maxElementsInMemory="20000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
</cache>
</ehcache>
2.2 CacheUtil 类
package pub.ehcache;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
import pub.servlet.ConfigInit;
import util.StringUtil;
/**
* ehcache工具类
* @author lee
* @since 2014-07-14
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class CacheUtil {
/**定义日志对象*/
private static Logger log = Logger.getLogger(CacheUtil.class);
/**
* 请求缓存超时毫秒数
*/
private static final long TIME_OUTS =
StringUtil.toInt(ConfigInit.getProperty("TC_TIMEOUTS", "60000"));
/**
* 执行缓存相关操作线程池
*/
private static ExecutorService exec = Executors.newCachedThreadPool();
/**
* cache名称
*/
private String cacheName;
/**
* manager
*/
private static CacheManager manager = CacheManager.create();
/**
* 根据name从manager中取cache
* @return Cache
*/
private Cache getCache(){
return manager.getCache(cacheName);
}
/**
* setter
* @param cacheName 缓存名
*/
public void setCacheName(String cacheName) {
this.cacheName = cacheName;
}
/**
* 外部接口,添加缓存至ehcache0
* @return CacheUtil
*/
public static CacheUtil getInstance(){
return new CacheUtil("ehcache0");
}
/**
* 单独配置一个缓存块,在ehcache.xml里需要配置
* @param cacheName 缓存名
* @return CacheUtil 返回
*/
public static CacheUtil getInstance(String cacheName){
return new CacheUtil(cacheName);
}
private CacheUtil(final String cacheName){
Callable call = new Callable(){
public Object call(){
setCacheName(cacheName);
Cache cache = manager.getCache(cacheName);
if(cache == null){
cache = new Cache(cacheName, 1000, false, false, 0, 0);
manager.addCache(cache);
}
return null;
}
};
execOperator(call);
}
/**
* 调用缓存操作超时则切换至数据库
* @param call 请求
* @return Object 返回类型
*/
private static Object execOperator(Callable call) {
Object obj = null;
try {
Future future = exec.submit(call);
obj = future.get(TIME_OUTS, TimeUnit.MILLISECONDS);
} catch (Exception e) {
//切换至数据库
log.info("*************读取缓存失败,切换至数据库**************");
return execOperator(call);
}
return obj;
}
/**
* put
* @param key 键
* @param value 对应值
*/
public void put(final String key, final Object value){
Callable call = new Callable(){
public Object call(){
getCache().put(new Element(key, value));
return null;
}
};
execOperator(call);
}
/**
* 线程内加入集合的值到cache
* @param map 需加入cache的键值对,key为String类型
*/
public void putAll(final Map map) {
if (!StringUtil.checkObj(map)) {
return;
}
Callable call = new Callable(){
public Object call() {
Cache curr = getCache();
for (Object obj : map.entrySet()) {
Entry entry = (Entry) obj;
curr.put(new Element(entry.getKey(), entry.getValue()));
}
return null;
}
};
execOperator(call);
}
/**
* get
* @param key 键值
* @return Object 返回对象类型
*/
public Object get(final String key){
Callable call = new Callable(){
public Object call(){
if(!getCache().isKeyInCache(key)){
return null;
}
return getCache().get(key).getValue();
}
};
return execOperator(call);
}
/**
* 线程内获取缓存中多个值
* @param keys 缓存的key,多个以逗号分隔
* @return Map键值对
*/
public Map getAll(final String keys) {
if (!StringUtil.checkStr(keys)) {
return null;
}
Callable call = new Callable(){
public Object call() {
Map result = new HashMap();
Cache curr = getCache();
String[] arr = keys.split(",");
String key = "";
for (int i = 0; i < arr.length; i++) {
key = arr[i];
if (curr.isKeyInCache(key)) {
result.put(key, curr.get(key).getValue());
}
}
return result;
}
};
return (Map) execOperator(call);
}
/**
* containsKey
* @param key 键值
* @return boolean 返回类型
*/
public boolean containsKey(final String key){
Callable call = new Callable(){
public Object call(){
return getCache().isKeyInCache(key);
}
};
return (Boolean)execOperator(call);
}
/**
* remove方法
* @param key 键值
*/
public void remove(final String key){
Callable call = new Callable(){
public Object call(){
if(getCache().isKeyInCache(key)){
getCache().remove(key);
}
return null;
}
};
execOperator(call);
}
/**
* removeAll
*/
public void removeAll(){
Callable call = new Callable(){
public Object call(){
getCache().removeAll();
return null;
}
};
execOperator(call);
}
/**
* flush
*
*/
public void flush(){
Callable call = new Callable(){
public Object call(){
getCache().flush();
return null;
}
};
execOperator(call);
}
/**
* close
*
*/
public void close(){
Callable call = new Callable(){
public Object call(){
manager.shutdown();
return null;
}
};
execOperator(call);
}
/**
* getKeys
* @return List
*/
public List getKeys(){
Callable call = new Callable(){
public Object call(){
return getCache().getKeys();
}
};
return (List) execOperator(call);
}
/**
* getCacheNames
* @return String[]
*/
public static String[] getCacheNames(){
Callable call = new Callable(){
public Object call(){
return manager.getCacheNames();
}
};
return (String[]) execOperator(call);
}
/**
* 装载缓存数据(1.来自数据库枚举,...等等)
*/
public static void initCacheFromDB(){
loadConfig();
}
/**
* 1,读取数据库
*/
private static void loadConfig() {
}
}