Cache缓存

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");
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值