java 缓存框架java caching system使用示例

  要使用java caching system,需要下面这几个包:jcs.jar,concurrent.jar,commons-logging.jar, commons-lang.jar,commons-collection.jar这几个包,在java工程里面,

首先新建一个使用jcs的配置文件:cache.ccf,文件名不能改。

# DEFAULT CACHE REGION # sets the default aux value for any non configured caches jcs.default= jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes jcs.default.cacheattributes.MaxObjects=2000 jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache jcs.default.elementattributes.IsEternal=false jcs.default.elementattributes.MaxLifeSeconds=3600 jcs.default.elementattributes.IdleTime=1800 jcs.default.elementattributes.IsSpool=true jcs.default.elementattributes.IsRemote=true jcs.default.elementattributes.IsLateral=true # CACHE REGIONS AVAILABLE # Regions preconfigured for caching jcs.region.bookCache= jcs.region.bookCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes jcs.region.bookCache.cacheattributes.MaxObjects=1200 jcs.region.bookCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache jcs.region.bookCache.elementattributes.IsEternal=false jcs.region.bookCache.elementattributes.MaxLifeSeconds=7200 jcs.region.bookCache.elementattributes.IdleTime=1800 jcs.region.bookCache.elementattributes.IsSpool=true jcs.region.bookCache.elementattributes.IsRemote=true jcs.region.bookCache.elementattributes.IsLateral=true # AUXILIARY CACHES AVAILABLE # Primary Disk Cache -- faster than the rest because of memory key storage jcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory jcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes jcs.auxiliary.DC.attributes.DiskPath=/usr/opt/bookstore/raf jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000 jcs.auxiliary.DC.attributes.MaxKeySize=10000 jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000 jcs.auxiliary.DC.attributes.MaxRecycleBinSize=7500

 

然后新建两个类,

import java.io.Serializable; import java.util.List; public class Student implements Serializable{ /** * */ public static final long serialVersionUID = 1L; public int sno; public String sname; public Student() {} public int getSno(){ return this.sno; } public void setSno(int no){ this.sno=no; } public String getSname(){ return this.sname; } public void setSname(String name){ this.sname=name; } }

 

import org.apache.jcs.JCS; public class StuObjManager { private static StuObjManager instance; private static int checkedOut=0; public static JCS stuCache; private StuObjManager() { try { stuCache=JCS.getInstance("stuCache"); } catch(Exception e) { e.printStackTrace(); } } public static StuObjManager getInstance() { synchronized(StuObjManager.class) { if(instance==null) { instance=new StuObjManager(); } } synchronized(instance) { instance.checkedOut++; } return instance; } public Student getStu(int sno) { return getStu(sno,true); } public Student getStu(int sno,boolean a) { Student stu=null; if(a) { stu=(Student)stuCache.get("Stu"+sno); } if(stu==null) { stu=loadStu(sno); } return stu; } public Student loadStu(int sno) { Student stu=new Student(); stu.sno=sno; try { boolean found=false; found=true; if(found) { stuCache.put("Stu"+sno, stu); } } catch(Exception e) {System.out.println("又报错了!");} return stu; } public void storeStu(Student stu) { try { if(stu.sno!=0) { stuCache.remove("Stu"+stu.sno); } stuCache.put("Stu"+stu.sno, stu); } catch(Exception e) {} } public static void main(String [] args) { long last=System.currentTimeMillis(); StuObjManager stu=StuObjManager.getInstance(); for(int i = 0; i < 2000; i++){ stu.loadStu(i); } Student stu1=stu.getStu(8,true); System.out.println(stu1.sno); System.out.println("所需时间:"+(System.currentTimeMillis()-last)); } }

 

运行类StuObjManager里面的main函数,结果如下:

8
所需时间:1250

 

你可能感兴趣的:(java 缓存框架java caching system使用示例)