冷静、沉淀

用apache JCS实现对象缓冲

上一篇 / 下一篇  2005-09-12 00:00:00 / 个人分类:J2EE

这两天客户抱怨在某些模块操作缓慢,通过使用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);
}

.................................


TAG:

引用 删除 Guest   /   2011-08-31 12:39:26
-3
引用 删除 sjk58   /   2011-08-31 12:39:12
引用 删除 Guest   /   2010-07-14 22:49:24
5
引用 删除 Guest   /   2010-06-24 15:12:46
5
引用 删除 CH_Go   /   2008-03-27 12:49:32
很实在!
引用 删除 Guest   /   2008-03-27 12:48:59
5
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2012-05-25  
  12345
6789101112
13141516171819
20212223242526
2728293031  

数据统计

  • 访问量: 79457
  • 日志数: 987
  • 图片数: 1
  • 建立时间: 2007-12-28
  • 更新时间: 2010-12-29

RSS订阅

Open Toolbar