配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="1200" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<!-- timeToIdleSeconds一直不访问这个对象的前提下,这个对象可以在cache中的存活时间 -->
<!-- timeToLiveSeconds无论对象访问或是不访问(闲置),这个对象在cache中的存活时间 -->
<!-- see 配置: http://haohaoxuexi.iteye.com/blog/2116749 -->
<cache name="session" maxEntriesLocalHeap="20000"
maxEntriesLocalDisk="20000" eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="3600" overflowToDisk="false" statistics="true">
</cache>
<cache name="moblie" maxEntriesLocalHeap="20000"
maxEntriesLocalDisk="20000" eternal="false" timeToIdleSeconds="70"
timeToLiveSeconds="70" overflowToDisk="false" statistics="true">
</cache>
</ehcache>
Java代码
package com.uaf.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import com.uaf.model.Session;
public class SessionCache {
private static Cache SESSION = CacheManage.getCache("session");
/**
* 通过openId获取一个自定义Session
* 不存在时会创建
* @param openId
* @return
*/
public static Session get(String openId) {
Element element = SESSION.get(openId);
Session session = null;
if (element == null || element.getObjectKey() == null) {
session = new Session();
SESSION.put(new Element(openId, session));
} else {
session = (Session) element.getObjectValue();
}
return session;
}
/**
* 放置一个
* @param key
* @param object
*/
public static void setAttribute(String key, Object object) {
SESSION.put(new Element(key, object));
}
/**
* 获取一个
* @param key
*
* @return
*/
public static Object getAttribute(String key) {
Element element = SESSION.get(key);
if (element == null || element.getObjectKey() == null) {
return null;
} else {
return element.getObjectValue();
}
}
/**
* 移除
* @param key
*/
public static void remove(String key) {
SESSION.remove(key);
}
}
package com.uaf.cache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
/**
* @Filename CacheManage.java
*
* @Description 缓存管理
*
* @author frank.tan 2015-9-7
*
*/
public class CacheManage {
/**
* 获取一个cache
* @param name
* @return
*/
public static Cache getCache(String name) {
return CacheManager.create().getCache(name);
}
}
使用
public CustomerInfo getBySession(String openId) {
JobLog.writeLog(MODULE, "getBySession传入的openId是:"+openId);
Assert.notNull(openId, "openId不能为null");
CustomerInfo customerInfo = null;
Session session = SessionCache.get(openId);
JobLog.writeLog(MODULE, "通过openId校验当前用户是否绑定城市传入的openId是:"+openId+session);
JobLog.writeLog(MODULE, "判断SessionCache.get(openId)是否为空:session:"+session);
Object object = session.getAttribute("customerInfo");
JobLog.writeLog(MODULE, "session.getAttribute(customerInfo)object是否为空:"+(object==null) + object);
if (object == null) {
synchronized (openId.intern()) {//TODO 防止相同openId的多次并发请求
session = SessionCache.get(openId);
JobLog.writeLog(MODULE, "SessionCache.get(openId)session是否为空:"+(session==null) + session);
object = session.getAttribute("customerInfo");
if (object == null) {
customerInfo = getByOpenId(openId);
JobLog.writeLog(MODULE, "getByOpenId(openId)customerInfo是否为空:"+(customerInfo==null) + customerInfo);
session.setAttribute("customerInfo", customerInfo);//入缓存
return customerInfo;
}
return (CustomerInfo) object;
}
}
customerInfo = (CustomerInfo) object;
JobLog.writeLog(MODULE, "(CustomerInfo) objectcustomerInfo是否为空:"+(customerInfo==null) + customerInfo);
return customerInfo;
}