溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中的緩存池有哪些

發(fā)布時(shí)間:2020-11-17 15:54:48 來源:億速云 閱讀:271 作者:Leah 欄目:編程語言

Java中的緩存池有哪些?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

緩存實(shí)體類CacheItem  中存放管理學(xué)生實(shí)體對象Student  ,緩存實(shí)體類CacheItem  存放在緩存池CachePool  中,MainTest  主要負(fù)責(zé)整體的測試工作。

緩存實(shí)體類

package com.paic.zhangqi.cache;  
import java.util.Date; 
 
/** 
 * 緩存實(shí)體 
 * @author ZHANGQI947 
 */ 
public class CacheItem {  
 // 創(chuàng)建緩存時(shí)間 
 private Date createTime = new Date();   
 // 緩存期滿時(shí)間 
 private long expireTime = 1;   
 // 緩存實(shí)體 
 private Object entity;    
 public CacheItem(Object obj, long expires) { 
  this.entity = obj; 
  this.expireTime = expires; 
 }   
 // 判斷緩存是否超時(shí) 
 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 { 
 // 緩存池唯一實(shí)例 
 private static CachePool instance; 
 // 緩存Map 
 private static Map<String, Object> cacheItems;   
 private CachePool() { 
  cacheItems = new HashMap<String, Object>(); 
 }   
 /** 
  * 獲取唯一的實(shí)例 
  * @return instance 
  */ 
 public synchronized static CachePool getInstance() { 
  if (instance == null) { 
   instance = new CachePool(); 
  } 
  return instance; 
 }   
 /** 
  * 清除所有的Item緩存 
  */ 
 public synchronized void clearAllItems() { 
  cacheItems.clear(); 
 }   
 /** 
  * 獲取緩存實(shí)例 
  * @param name 緩存名稱 
  * @return 緩存實(shí)例 
  */ 
 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 實(shí)例對象 
  * @param expires 超時(shí)時(shí)長 
  */ 
 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); 
 } 
 /** 
  * 移除緩存數(shù)據(jù) 
  * @param name 
  */ 
 public synchronized void removeCacheItem(String name) { 
  if (!cacheItems.containsKey(name)) { 
   return ; 
  } 
  cacheItems.remove(name); 
 }   
 /** 
  * 獲取緩存數(shù)據(jù)的數(shù)量 
  * @return 
  */ 
 public int getSize() { 
  return cacheItems.size(); 
 }  
} 

學(xué)生類Student

package com.paic.zhangqi.cache; 
/** 
 * 學(xué)生類 
 * @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);    
  // 設(shè)置線程休眠,其中002對象會超時(shí) 
  Thread.sleep(200);    
  Student stu001 = (Student) cachePool.getCacheItem("001"); 
  if (null != stu001) { 
   System.out.println(stu001.getName()); 
  }    
  // 由于超時(shí),這里取出的002對象為null 
  Student stu002 = (Student) cachePool.getCacheItem("002"); 
  if (null != stu002) { 
   System.out.println(stu002.getName()); 
  }    
  // 獲取打印緩存池中對象數(shù)量 
  int cacheSize = cachePool.getSize(); 
  System.out.println(cacheSize);    
  // 刪除對象002 
  cachePool.removeCacheItem("002");   
  // 打印緩存池?cái)?shù)量 
  cacheSize = cachePool.getSize(); 
  System.out.println(cacheSize); 
 } 
} 

測試結(jié)果

l1 

3

關(guān)于Java中的緩存池有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI