JCS在一般用在,查询数据库比较频繁,每次查询的结果都差不多,这样讲数据进行缓冲,可以 减轻数据库负担:
下面举个例子:
在一个点击量比较频繁的页面,调用一个数据列表,每个用户进去在某一时间段数据都一样,这时将考虑将数据进行缓冲。
1、配置 JCS cache.ccf 文件,将该文件直接放到src根目录下即可:
java 代码
- /**缓冲区名称**/
- jcs.region.chatCache=DC
- /****/
- jcs.region.chatCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
- /**缓冲区的大小为存放1000个对象**/
- jcs.region.chatCache.cacheattributes.MaxObjects=10000
- /**内存缓冲器使用LRUMemoryCache对象**/
- jcs.region.chatCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
- /****/
- jcs.region.chatCache.cacheattributes.UseMemoryShrinker=true
- /**指明对象超过2000秒则过期**/
- jcs.region.chatCache.cacheattributes.MaxMemoryIdleTimeSeconds=2000
- /**每隔60秒检查一次**/
- jcs.region.chatCache.cacheattributes.ShrinkerIntervalSeconds=60
- /****/
- jcs.region.chatCache.cacheattributes.MaxSpoolPerRun=500
- /****/
- jcs.region.chatCache.elementattributes=org.apache.jcs.engine.ElementAttributes
- /****/
- jcs.region.chatCache.elementattributes.IsEternal=false
- /****/
- jcs.region.chatCache.elementattributes.IsSpool=true
- /****/
- jcs.region.chatCache.elementattributes.IsRemote=false
- /****/
- jcs.region.chatCache.elementattributes.IsLateral=true
- /****/
- jcs.region.chatCache.elementattributes.MaxLifeSeconds=3600
2、程序中这样应用即可:
- JCS chatCache = JCS.getInstance("chatCache"); //获得JCS缓冲区对象
- String chatCacheKey = "chat_cache_"+roomID; // 构建key
- if(chatCache.get(chatCacheKey)!=null){
- roomintroList = (List)chatCache.get(chatCacheKey);
- }else{
- roomintroList = 数据库查询;
- chatCache.put(chatCacheKey, roomintroList);
- }
336

被折叠的 条评论
为什么被折叠?



