用apache JCS实现对象缓冲

这两天客户抱怨在某些模块操作缓慢,通过使用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博客 ” ,链接:http://blog.itpub.net/6906/viewspace-21891/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-21891/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值