这两天客户抱怨在某些模块操作缓慢,通过使用JCS对对象实现缓存,有效的提高了执行速度.
JCS的配置很简单:
1.在WEB-INF/classes中加入配置文件,cache.ccf,内容如下:
jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=100
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
#时间过期
#过期时间为300秒,每300秒检查一次
jcs.default.cacheattributes.cacheattributes.UseMemoryShrinker=true
jcs.default.cacheattributes.cacheattributes.MaxMemoryIdleTimeSeconds=300
jcs.default.cacheattributes.cacheattributes.ShrinkerIntervalSeconds=300
jcs.region.unitCache=
jcs.region.unitCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.unitCache.cacheattributes.MaxObjects=100
jcs.region.unitCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
#时间过期
#过期时间为300秒,每300秒检查一次
jcs.unitCache.cacheattributes.cacheattributes.UseMemoryShrinker=true
jcs.unitCache.cacheattributes.cacheattributes.MaxMemoryIdleTimeSeconds=300
jcs.unitCache.cacheattributes.cacheattributes.ShrinkerIntervalSeconds=300
2.在类中定义缓存变量,
//对象缓冲池
private static JCS unitCache;
并在构造函数中获取cache:
try {
unitCache = JCS.getInstance("unitCache");
} catch (Exception e) {
log.error("获取对象缓存出错!" + e);
}
3.这样就可以使用cache了:
if (!CommonUtility.isNull(unitCache)) {
List lstCache = (List) unitCache.get("unitList");
if (!CommonUtility.isNull(lstCache))
return lstCache;
}
log.info("不能从缓冲中获取对象,通过业务方法获取!");
//do something here.
//加入缓冲机制
//把列表加入缓存中
if (!CommonUtility.isNull(unitCache)) {
log.info("把列表放入缓冲池中!");
unitCache.put("unitList", list);
}
.................................
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/6906/viewspace-21891/ ,如需转载,请注明出处,否则将追究法律责任。