ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="com.bb2c.common.cache.CacheListener"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="BB2CParamCache" maxElementsInMemory="10000" eternal="true"
overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
public class BB2CCacheManager {
private static final Cache cache;
static {
CacheManager manager = new CacheManager();
cache = manager.getCache("BB2CParamCache");
}
public static void addCache(String type, List list) {
Element e = new Element(type, list);
cache.put(e);
}
public static List getParamList(String type) {
Element e = cache.get(type);
List list = (List) e.getValue();
return list;
}
}
public class StartupListener extends ContextLoaderListener implements
ServletContextListener {
private static Log log = LogFactory.getLog(StartupListener.class);
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
setupContext(servletContext);
}
public static void setupContext(ServletContext context) {
ApplicationContext applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
if (log.isDebugEnabled()) {
log.debug("后台管理系统开始启动,并开始加载参数列表............. [OK]");
}
IParamService paramService = (IParamService) applicationContext
.getBean(Constants.PARAM_BEAN);
List<ParamSetObj> newList = new ArrayList<ParamSetObj>();
String type = null;
try {
// 加载参数列表
List list = paramService.queryParamListForCache();
// 将参数列表分类放入缓存
for (int i = 0; i < list.size(); i++) {
ParamSetObj param = (ParamSetObj) list.get(i);
if (i == 0) {
type = param.getParamType();
}
if (!param.getParamType().equals(type)) {
BB2CCacheManager.addCache(type, newList);
type = param.getParamType();
newList = new ArrayList<ParamSetObj>();
newList.add(param);
} else {
newList.add(param);
}
if(i==list.size()-1){
BB2CCacheManager.addCache(type, newList);
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("加载参数列表错误!....................[fail]");
}
if (log.isDebugEnabled()) {
log.debug("后台管理系统启动完毕,参数列别已近加载完毕............. [OK]");
}
}
}
public class CacheListener extends CacheManagerEventListenerFactory {
public CacheManagerEventListener createCacheManagerEventListener(
Properties properties) {
return new CMEL();
}
}
class CMEL implements CacheManagerEventListener {
public void dispose() throws CacheException {
}
public Status getStatus() {
return null;
}
public void init() throws CacheException {
}
public void notifyCacheAdded(String cacheName) {
System.out.println("Cache [" + cacheName + "] Added");
}
public void notifyCacheRemoved(String cacheName) {
System.out.println("Cache [" + cacheName + "] Deleted");
}
}
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="com.bb2c.common.cache.CacheListener"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="BB2CParamCache" maxElementsInMemory="10000" eternal="true"
overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
public class BB2CCacheManager {
private static final Cache cache;
static {
CacheManager manager = new CacheManager();
cache = manager.getCache("BB2CParamCache");
}
public static void addCache(String type, List list) {
Element e = new Element(type, list);
cache.put(e);
}
public static List getParamList(String type) {
Element e = cache.get(type);
List list = (List) e.getValue();
return list;
}
}
public class StartupListener extends ContextLoaderListener implements
ServletContextListener {
private static Log log = LogFactory.getLog(StartupListener.class);
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
setupContext(servletContext);
}
public static void setupContext(ServletContext context) {
ApplicationContext applicationContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(context);
if (log.isDebugEnabled()) {
log.debug("后台管理系统开始启动,并开始加载参数列表............. [OK]");
}
IParamService paramService = (IParamService) applicationContext
.getBean(Constants.PARAM_BEAN);
List<ParamSetObj> newList = new ArrayList<ParamSetObj>();
String type = null;
try {
// 加载参数列表
List list = paramService.queryParamListForCache();
// 将参数列表分类放入缓存
for (int i = 0; i < list.size(); i++) {
ParamSetObj param = (ParamSetObj) list.get(i);
if (i == 0) {
type = param.getParamType();
}
if (!param.getParamType().equals(type)) {
BB2CCacheManager.addCache(type, newList);
type = param.getParamType();
newList = new ArrayList<ParamSetObj>();
newList.add(param);
} else {
newList.add(param);
}
if(i==list.size()-1){
BB2CCacheManager.addCache(type, newList);
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("加载参数列表错误!....................[fail]");
}
if (log.isDebugEnabled()) {
log.debug("后台管理系统启动完毕,参数列别已近加载完毕............. [OK]");
}
}
}
public class CacheListener extends CacheManagerEventListenerFactory {
public CacheManagerEventListener createCacheManagerEventListener(
Properties properties) {
return new CMEL();
}
}
class CMEL implements CacheManagerEventListener {
public void dispose() throws CacheException {
}
public Status getStatus() {
return null;
}
public void init() throws CacheException {
}
public void notifyCacheAdded(String cacheName) {
System.out.println("Cache [" + cacheName + "] Added");
}
public void notifyCacheRemoved(String cacheName) {
System.out.println("Cache [" + cacheName + "] Deleted");
}
}