项目是一个门户网站,一些新闻等会做缓存.
导入jar包:
项目跟目录下配置文件:cache.ccf
#内存缓存
jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
#过期时间设定
#jcs.default.cacheattributes.cacheattributes.UseMemoryShrinker=true
#jcs.default.cacheattributes.cacheattributes.MaxMemoryIdleTimeSeconds=3600
#jcs.default.cacheattributes.cacheattributes.ShrinkerIntervalSeconds=60
自己的工具类:JCSManager
package coffee.util.jcs; import org.apache.jcs.JCS; import org.apache.jcs.access.exception.CacheException; import org.apache.log4j.Logger; public class JCSManager { private static JCSManager instance; private static int checkedOut=0; private static JCS ObjCache; static Logger log =Logger.getLogger(JCSManager.class); private JCSManager(){ try { ObjCache=JCS.getInstance("dc");//这个dc相当于一个regions } catch (CacheException e) { e.printStackTrace(); } } public static JCSManager getInstance(){ synchronized(JCSManager.class){ if(instance==null){ instance= new JCSManager(); } } synchronized(instance){ instance.checkedOut++; } return instance; } //取出缓存 public Object getObj(Object key){ Object obj=null; obj=(Object)ObjCache.get(key); return obj; } //放入缓存 public void storeObject(Object key,Object obj){ try { ObjCache.put(key, obj); } catch (CacheException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //删除缓存 public void removeObject(Object key){ try { ObjCache.remove(key); } catch (CacheException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //清除所有缓存 public void clear(){ try { ObjCache.clear(); } catch (CacheException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }