ITeye博客

JCS 应用

fzfx88 · 2007-12-10 17:38 · JCS · Markdown

JCS在一般用在,查询数据库比较频繁,每次查询的结果都差不多,这样讲数据进行缓冲,可以 减轻数据库负担:
下面举个例子:
在一个点击量比较频繁的页面,调用一个数据列表,每个用户进去在某一时间段数据都一样,这时将考虑将数据进行缓冲。
1、配置 JCS cache.ccf 文件,将该文件直接放到src根目录下即可:

java 代码

  1. /**缓冲区名称**/
  2. jcs.region.chatCache=DC
  3. /****/
  4. jcs.region.chatCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
  5. /**缓冲区的大小为存放1000个对象**/
  6. jcs.region.chatCache.cacheattributes.MaxObjects=10000
  7. /**内存缓冲器使用LRUMemoryCache对象**/
  8. jcs.region.chatCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
  9. /****/
  10. jcs.region.chatCache.cacheattributes.UseMemoryShrinker=true
  11. /**指明对象超过2000秒则过期**/
  12. jcs.region.chatCache.cacheattributes.MaxMemoryIdleTimeSeconds=2000
  13. /**每隔60秒检查一次**/
  14. jcs.region.chatCache.cacheattributes.ShrinkerIntervalSeconds=60
  15. /****/
  16. jcs.region.chatCache.cacheattributes.MaxSpoolPerRun=500
  17. /****/
  18. jcs.region.chatCache.elementattributes=org.apache.jcs.engine.ElementAttributes
  19. /****/
  20. jcs.region.chatCache.elementattributes.IsEternal=false
  21. /****/
  22. jcs.region.chatCache.elementattributes.IsSpool=true
  23. /****/
  24. jcs.region.chatCache.elementattributes.IsRemote=false
  25. /****/
  26. jcs.region.chatCache.elementattributes.IsLateral=true
  27. /****/
  28. jcs.region.chatCache.elementattributes.MaxLifeSeconds=3600

2、程序中这样应用即可:

  1. JCS chatCache  = JCS.getInstance("chatCache");    //获得JCS缓冲区对象
  2. String chatCacheKey = "chat_cache_"+roomID;  // 构建key
  3. if(chatCache.get(chatCacheKey)!=null){
  4. roomintroList = (List)chatCache.get(chatCacheKey);
  5. }else{
  6. roomintroList = 数据库查询;
  7. chatCache.put(chatCacheKey, roomintroList);
  8. }

评论(1)

  1. #1 楼 · 悬空90 · 2011-11-29 15:13

    谢谢分享 呵呵

评论功能维护中,当前仅展示历史评论。