Java中的缓存池有哪些?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
缓存实体类CacheItem 中存放管理学生实体对象Student ,缓存实体类CacheItem 存放在缓存池CachePool 中,MainTest 主要负责整体的测试工作。
<强>缓存实体类强>
包com.paic.zhangqi.cache; 进口java.util.Date;/* * *缓存实体 * @author ZHANGQI947 */公开课CacheItem {//创建缓存时间 私人约会createTime=new日期();//缓存期满时间 私人长expireTime=1;//缓存实体 私有对象实体; 公共CacheItem(对象obj,长到期){ 这一点。实体=obj; 这一点。expireTime=到期; }//判断缓存是否超时 公共布尔isExpired () { 返回(expireTime !=1,,新的日期().getTime ()——createTime.getTime()比;expireTime); } 上市日期getCreateTime () { 返回createTime; } 公共空间setCreateTime(日期createTime) { 这一点。createTime=createTime; } 公共对象getEntity () { 返回的实体; } 公共空间setEntity(对象实体){ 这一点。实体=实体; } 公共长getExpireTime () { 返回expireTime; } 公共空间setExpireTime(长expireTime) { 这一点。expireTime=expireTime; } }
<>强缓存池CachePool 强>
包com.paic.zhangqi.cache; 进口java.util.Date; 进口java.util.HashMap; 进口java.util.Map;/* * *缓存池 * @author管理员 */公开课CachePool {//缓存池唯一实例 私有静态CachePool实例;//缓存地图 私有静态Map<字符串,Object>cacheItems; 私人CachePool () { cacheItems=new HashMap<字符串,Object> (); }/* * *获取唯一的实例 * @return实例 */公共静态同步CachePool getInstance () { 如果(实例==null) { 实例=new CachePool (); } 返回实例; }/* * *清除所有的项缓存 */公共同步空白clearAllItems () { cacheItems.clear (); }/* * *获取缓存实例 * @param名字缓存名称 * @return缓存实例 */公共getCacheItem同步对象(字符串名称){ 如果(! cacheItems.containsKey(名称)){ 返回null; } CacheItem CacheItem=(CacheItem) cacheItems.get(名称); 如果(cacheItem.isExpired ()) { 返回null; } 返回cacheItem.getEntity (); }/* * *存放缓存信息 * @param名字名称 * @param obj实例对象 * @param超到期时时长 */公共同步空白putCacheItem(字符串名称、对象obj长到期){//判断该对象是否在在缓存池,不在直接 如果(! cacheItems.containsKey(名称)){ cacheItems。把(名字,新的CacheItem (obj,到期)); }//获取缓存池中对象,更新对象信息 CacheItem CacheItem=(CacheItem) cacheItems.get(名称); cacheItem。setCreateTime(新日期()); cacheItem.setEntity (obj); cacheItem.setExpireTime(过期); }/* * *移除缓存数据 * @param名字 */公共同步空白removeCacheItem(字符串名称){ 如果(! cacheItems.containsKey(名称)){ 返回; } cacheItems.remove(名称); }/* * *获取缓存数据的数量 * @return */公共int getSize () { 返回cacheItems.size (); } }
<>强学生类学生强>
包com.paic.zhangqi.cache;/* * *学生类 * @author管理员 */公开课学生{ 私人字符串名称; 私人字符串id; 私人int年龄; 私人int sal; 公共的学生(){ } 公共学生(字符串名称字符串id, int, int sal) { this.name=名称; 这一点。id=id; 这一点。年龄=年龄; 这一点。萨尔=sal; } 公共字符串getName () { 返回名称; } 公共空间setName(字符串名称){ this.name=名称; } 公共字符串getId () { 返回id; } 公共空间setId (String id) { 这一点。id=id; } 公共int getAge () { 返回年龄; } 公共空间setAge (int年龄){ 这一点。年龄=年龄; } 公共int getSal () { 返回sal; } 公共空间setSal (int sal) { 这一点。萨尔=sal; } }
<>强主测试类maint 强>
包com.paic.zhangqi.cache;/* * *主测试类 * @author ZHANGQI947 */公开课maint {/* * * @param arg游戏 * @throws InterruptedException */公共静态void main (String [] args)抛出InterruptedException {//获取缓存池 CachePool CachePool=CachePool.getInstance (); 学生stu1=新学生(“l1",“stu001", 25岁,40); 学生stu2=新学生(“l2",“stu002", 25岁,40); 学生stu3=新学生(“l3",“stu003", 25岁,40); 学生stu4=新学生(“l4",“stu004", 25岁,40); stu1 cachePool.putCacheItem (“001“, 122222); cachePool.putCacheItem (“002“stu2 10); stu3 cachePool.putCacheItem (“003“, 360002); stu4 cachePool.putCacheItem (“004“, 1222222);//设置线程休眠,其中002对象会超时 thread . sleep (200); 学生stu001=(学生)cachePool.getCacheItem (“001”); 如果(零!=stu001) { System.out.println (stu001.getName ()); }//由于超时,这里取出的002对象为null 学生stu002=(学生)cachePool.getCacheItem (“002”); 如果(零!=stu002) { System.out.println (stu002.getName ()); }//获取打印缓存池中对象数量 int cacheSize=cachePool.getSize (); System.out.println (cacheSize); 002年//删除对象 cachePool.removeCacheItem (“002”);//打印缓存池数量 cacheSize=cachePool.getSize (); System.out.println (cacheSize); } }Java中的缓存池有哪些