JCS 应用

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值