欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java缓存池代码实例详解

程序员文章站 2024-02-28 11:58:04
这篇实例中有四个类,分别为 cacheitem  缓存实体类 cachepool  缓存池 student  学生实体类...

这篇实例中有四个类,分别为

cacheitem  缓存实体类

cachepool  缓存池

student  学生实体类

maintest  主测试类

其中,缓存实体类cacheitem  中存放管理学生实体对象student  ,缓存实体类cacheitem  存放在缓存池cachepool  中,maintest  主要负责整体的测试工作。

缓存实体类

package com.paic.zhangqi.cache;  
import java.util.date; 
 
/** 
 * 缓存实体 
 * @author zhangqi947 
 */ 
public class cacheitem {  
 // 创建缓存时间 
 private date createtime = new date();   
 // 缓存期满时间 
 private long expiretime = 1;   
 // 缓存实体 
 private object entity;    
 public cacheitem(object obj, long expires) { 
  this.entity = obj; 
  this.expiretime = expires; 
 }   
 // 判断缓存是否超时 
 public boolean isexpired() { 
  return (expiretime != -1 && new date().gettime() - createtime.gettime() > expiretime); 
 }  
 public date getcreatetime() { 
  return createtime; 
 }  
 public void setcreatetime(date createtime) { 
  this.createtime = createtime; 
 }  
 public object getentity() { 
  return entity; 
 }  
 public void setentity(object entity) { 
  this.entity = entity; 
 }  
 public long getexpiretime() { 
  return expiretime; 
 }  
 public void setexpiretime(long expiretime) { 
  this.expiretime = expiretime; 
 } 
} 

缓存池cachepool

package com.paic.zhangqi.cache;  
import java.util.date; 
import java.util.hashmap; 
import java.util.map; 
/** 
 * 缓存池 
 * @author administrator 
 */ 
public class cachepool { 
 // 缓存池唯一实例 
 private static cachepool instance; 
 // 缓存map 
 private static map<string, object> cacheitems;   
 private cachepool() { 
  cacheitems = new hashmap<string, object>(); 
 }   
 /** 
  * 获取唯一的实例 
  * @return instance 
  */ 
 public synchronized static cachepool getinstance() { 
  if (instance == null) { 
   instance = new cachepool(); 
  } 
  return instance; 
 }   
 /** 
  * 清除所有的item缓存 
  */ 
 public synchronized void clearallitems() { 
  cacheitems.clear(); 
 }   
 /** 
  * 获取缓存实例 
  * @param name 缓存名称 
  * @return 缓存实例 
  */ 
 public synchronized object getcacheitem(string name) { 
  if (!cacheitems.containskey(name)) { 
   return null; 
  } 
  cacheitem cacheitem = (cacheitem) cacheitems.get(name); 
  if (cacheitem.isexpired()) { 
   return null; 
  } 
  return cacheitem.getentity(); 
 } 
 /** 
  * 存放缓存信息 
  * @param name 名称 
  * @param obj 实例对象 
  * @param expires 超时时长 
  */ 
 public synchronized void putcacheitem(string name, object obj, long expires) { 
  // 判断该对象是否在在缓存池,不在直接put 
  if (!cacheitems.containskey(name)) { 
   cacheitems.put(name, new cacheitem(obj, expires)); 
  } 
  // 获取缓存池中对象,更新对象信息 
  cacheitem cacheitem = (cacheitem) cacheitems.get(name); 
  cacheitem.setcreatetime(new date()); 
  cacheitem.setentity(obj); 
  cacheitem.setexpiretime(expires); 
 } 
 /** 
  * 移除缓存数据 
  * @param name 
  */ 
 public synchronized void removecacheitem(string name) { 
  if (!cacheitems.containskey(name)) { 
   return ; 
  } 
  cacheitems.remove(name); 
 }   
 /** 
  * 获取缓存数据的数量 
  * @return 
  */ 
 public int getsize() { 
  return cacheitems.size(); 
 }  
} 

学生类student

package com.paic.zhangqi.cache; 
/** 
 * 学生类 
 * @author administrator 
 */ 
public class student {  
 private string name; 
 private string id; 
 private int age; 
 private int sal; 
 public student() { 
   
 }   
 public student(string name, string id, int age, int sal) { 
  this.name = name; 
  this.id = id; 
  this.age = age; 
  this.sal = sal; 
 }  
 public string getname() { 
  return name; 
 }  
 public void setname(string name) { 
  this.name = name; 
 }  
 public string getid() { 
  return id; 
 }  
 public void setid(string id) { 
  this.id = id; 
 }  
 public int getage() { 
  return age; 
 }  
 public void setage(int age) { 
  this.age = age; 
 }  
 public int getsal() { 
  return sal; 
 } 
 public void setsal(int sal) { 
  this.sal = sal; 
 } 
}

主测试类maintest

package com.paic.zhangqi.cache; 
/** 
 * 主测试类 
 * @author zhangqi947 
 */ 
public class maintest { 
 
 /** 
  * @param args 
  * @throws interruptedexception 
  */ 
 public static void main(string[] args) throws interruptedexception { 
  // 获取缓存池 
  cachepool cachepool = cachepool.getinstance();    
  student stu1 = new student("l1", "stu001", 25, 40); 
  student stu2 = new student("l2", "stu002", 25, 40); 
  student stu3 = new student("l3", "stu003", 25, 40); 
  student stu4 = new student("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);    
  student stu001 = (student) cachepool.getcacheitem("001"); 
  if (null != stu001) { 
   system.out.println(stu001.getname()); 
  }    
  // 由于超时,这里取出的002对象为null 
  student stu002 = (student) cachepool.getcacheitem("002"); 
  if (null != 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); 
 } 
} 

测试结果

l1 

3

希望本篇文章内容对您有所帮助