Java缓存池代码实例详解
这篇实例中有四个类,分别为
CacheItem 缓存实体类
CachePool 缓存池
Student 学生实体类
MainTest 主测试类
其中,缓存实体类CacheItem 中存放管理学生实体对象Student ,缓存实体类CacheItem 存放在缓存池CachePool 中,MainTest 主要负责整体的测试工作。
缓存实体类
packagecom.paic.zhangqi.cache; importjava.util.Date; /** *缓存实体 *@authorZHANGQI947 */ publicclassCacheItem{ //创建缓存时间 privateDatecreateTime=newDate(); //缓存期满时间 privatelongexpireTime=1; //缓存实体 privateObjectentity; publicCacheItem(Objectobj,longexpires){ this.entity=obj; this.expireTime=expires; } //判断缓存是否超时 publicbooleanisExpired(){ return(expireTime!=-1&&newDate().getTime()-createTime.getTime()>expireTime); } publicDategetCreateTime(){ returncreateTime; } publicvoidsetCreateTime(DatecreateTime){ this.createTime=createTime; } publicObjectgetEntity(){ returnentity; } publicvoidsetEntity(Objectentity){ this.entity=entity; } publiclonggetExpireTime(){ returnexpireTime; } publicvoidsetExpireTime(longexpireTime){ this.expireTime=expireTime; } }
缓存池CachePool
packagecom.paic.zhangqi.cache; importjava.util.Date; importjava.util.HashMap; importjava.util.Map; /** *缓存池 *@authorAdministrator */ publicclassCachePool{ //缓存池唯一实例 privatestaticCachePoolinstance; //缓存Map privatestaticMapcacheItems; privateCachePool(){ cacheItems=newHashMap (); } /** *获取唯一的实例 *@returninstance */ publicsynchronizedstaticCachePoolgetInstance(){ if(instance==null){ instance=newCachePool(); } returninstance; } /** *清除所有的Item缓存 */ publicsynchronizedvoidclearAllItems(){ cacheItems.clear(); } /** *获取缓存实例 *@paramname缓存名称 *@return缓存实例 */ publicsynchronizedObjectgetCacheItem(Stringname){ if(!cacheItems.containsKey(name)){ returnnull; } CacheItemcacheItem=(CacheItem)cacheItems.get(name); if(cacheItem.isExpired()){ returnnull; } returncacheItem.getEntity(); } /** *存放缓存信息 *@paramname名称 *@paramobj实例对象 *@paramexpires超时时长 */ publicsynchronizedvoidputCacheItem(Stringname,Objectobj,longexpires){ //判断该对象是否在在缓存池,不在直接put if(!cacheItems.containsKey(name)){ cacheItems.put(name,newCacheItem(obj,expires)); } //获取缓存池中对象,更新对象信息 CacheItemcacheItem=(CacheItem)cacheItems.get(name); cacheItem.setCreateTime(newDate()); cacheItem.setEntity(obj); cacheItem.setExpireTime(expires); } /** *移除缓存数据 *@paramname */ publicsynchronizedvoidremoveCacheItem(Stringname){ if(!cacheItems.containsKey(name)){ return; } cacheItems.remove(name); } /** *获取缓存数据的数量 *@return */ publicintgetSize(){ returncacheItems.size(); } }
学生类Student
packagecom.paic.zhangqi.cache; /** *学生类 *@authorAdministrator */ publicclassStudent{ privateStringname; privateStringid; privateintage; privateintsal; publicStudent(){ } publicStudent(Stringname,Stringid,intage,intsal){ this.name=name; this.id=id; this.age=age; this.sal=sal; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicintgetSal(){ returnsal; } publicvoidsetSal(intsal){ this.sal=sal; } }
主测试类MainTest
packagecom.paic.zhangqi.cache; /** *主测试类 *@authorZHANGQI947 */ publicclassMainTest{ /** *@paramargs *@throwsInterruptedException */ publicstaticvoidmain(String[]args)throwsInterruptedException{ //获取缓存池 CachePoolcachePool=CachePool.getInstance(); Studentstu1=newStudent("l1","stu001",25,40); Studentstu2=newStudent("l2","stu002",25,40); Studentstu3=newStudent("l3","stu003",25,40); Studentstu4=newStudent("l4","stu004",25,40); cachePool.putCacheItem("001",stu1,122222); cachePool.putCacheItem("002",stu2,10); cachePool.putCacheItem("003",stu3,360002); cachePool.putCacheItem("004",stu4,1222222); //设置线程休眠,其中002对象会超时 Thread.sleep(200); Studentstu001=(Student)cachePool.getCacheItem("001"); if(null!=stu001){ System.out.println(stu001.getName()); } //由于超时,这里取出的002对象为null Studentstu002=(Student)cachePool.getCacheItem("002"); if(null!=stu002){ System.out.println(stu002.getName()); } //获取打印缓存池中对象数量 intcacheSize=cachePool.getSize(); System.out.println(cacheSize); //删除对象002 cachePool.removeCacheItem("002"); //打印缓存池数量 cacheSize=cachePool.getSize(); System.out.println(cacheSize); } }
测试结果
l1
4
3
希望本篇文章内容对您有所帮助