|
|
要使用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 cachesjcs.default=jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributesjcs.default.cacheattributes.MaxObjects=2000jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCachejcs.default.elementattributes.IsEternal=falsejcs.default.elementattributes.MaxLifeSeconds=3600jcs.default.elementattributes.IdleTime=1800jcs.default.elementattributes.IsSpool=truejcs.default.elementattributes.IsRemote=truejcs.default.elementattributes.IsLateral=true# CACHE REGIONS AVAILABLE# Regions preconfigured for cachingjcs.region.bookCache=jcs.region.bookCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributesjcs.region.bookCache.cacheattributes.MaxObjects=1200jcs.region.bookCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCachejcs.region.bookCache.elementattributes.IsEternal=falsejcs.region.bookCache.elementattributes.MaxLifeSeconds=7200jcs.region.bookCache.elementattributes.IdleTime=1800jcs.region.bookCache.elementattributes.IsSpool=truejcs.region.bookCache.elementattributes.IsRemote=truejcs.region.bookCache.elementattributes.IsLateral=true# AUXILIARY CACHES AVAILABLE# Primary Disk Cache -- faster than the rest because of memory key storagejcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactoryjcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributesjcs.auxiliary.DC.attributes.DiskPath=/usr/opt/bookstore/rafjcs.auxiliary.DC.attributes.MaxPurgatorySize=10000jcs.auxiliary.DC.attributes.MaxKeySize=10000jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000jcs.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 |
|