---
title: "JCS 应用"
author: "fzfx88"
date: "2007-12-10T17:38:28Z"
tags: []
slug: "jcs"
draft: false
lastmod: "2007-12-10T17:38:28Z"
id: 147346
url: "https://fzfx88.iteye.com/blog/147346"
---

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

java 代码

01. /\*\*缓冲区名称\*\*/
02. jcs.region.chatCache=DC
03. /\*\*\*\*/
04. jcs.region.chatCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
05. /\*\*缓冲区的大小为存放1000个对象\*\*/
06. jcs.region.chatCache.cacheattributes.MaxObjects=10000
07. /\*\*内存缓冲器使用LRUMemoryCache对象\*\*/
08. jcs.region.chatCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
09. /\*\*\*\*/
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. }