package person.cobee.middleware.cache.java;
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* 自研Java緩存管理器
* 1, 按照JSR-107規範實現
* 2,使用ConcurrentHashMap結構存儲數據,並且value值使用SoftReference包裝。
* 3,緩存定義超時時間,開啓後臺線程定時掃描清理超時的數據
*
* @author cobee
* @since 2020-02-07
*/
public class MyCacheManager {
/**
* 定義多長時間描掃一次過期的數據,默認爲5秒
*/
private static final long CLEAN_UP_PERIOD_IN_SEC = 5;
/**
* 使用Map數據結構存儲緩存數據,key是字符串類型,value是軟引用類型,當內存確實不夠用的時候,jvm會回收對應的value引用對象
*/
private final ConcurrentHashMap<String, SoftReference<Cache>> cacheMap = new ConcurrentHashMap<>();
public MyCacheManager(){
// 定義一個定時任務,清理已過期的緩存數據
Thread cleanerThread = new Thread(() -> {
while(!Thread.currentThread().isInterrupted()){
try {
Thread.sleep(CLEAN_UP_PERIOD_IN_SEC * 1000); // 每5秒清理緩存一次
for(Map.Entry<String, SoftReference<Cache>> entry : cacheMap.entrySet()){
Cache cache = entry.getValue().get();
if(cache.isExpiry()){
cacheMap.remove(entry.getKey());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
});
cleanerThread.setDaemon(true);
cleanerThread.start();
}
/**
* 清除所有緩存
*/
public void clear(){
cacheMap.clear();
}
/**
* 返回緩存管理器裏面緩存的元素的個數
* @return
*/
public long size(){
if(cacheMap.isEmpty()){
return 0L;
}
Set<Map.Entry<String, SoftReference<Cache>>> entrySet = cacheMap.entrySet();
long count = 0L;
for(Map.Entry<String, SoftReference<Cache>> entry : entrySet){
SoftReference<Cache> value = entry.getValue();
Cache cache = value.get();
if(!cache.isExpiry()){
count++;
}
}
return count;
}
/**
* 增加或更新一個緩存數據
* @param key
* @param value
* @param expiryTime -1表示不超期,單位爲毫秒
* @return 返回null或者舊的緩存值
*/
public Object put(String key, Object value, long expiryTime){
if(key == null){
return null;
}
if(value == null){
return extractObjVal(cacheMap.remove(key));
}
if(expiryTime <= 0){
expiryTime = -1;
}else{
expiryTime = System.currentTimeMillis() + expiryTime;
}
return extractObjVal(cacheMap.put(key, new SoftReference<>(new Cache(value, expiryTime))));
}
/**
* 刪除緩存
* @param key
* @return
*/
public Object remove(String key){
if(key == null){
return null;
}
return extractObjVal(cacheMap.remove(key));
}
/**
* 從緩存中獲取值,沒有找到返回null,找到但已超期也返回null。
* @param key
* @return
*/
public Object get(String key){
if(key == null){
return null;
}
SoftReference<Cache> softReference = cacheMap.get(key);
if(softReference == null){
return null;
}else{
Cache cache = softReference.get();
if(cache.isExpiry()){
cacheMap.remove(key); // 訪問的時候被動刪除超期的元素
return null;
}else{
return cache.getValue();
}
}
}
/**
* 抽取cache對象裏面的緩存值
* @param cacheSoftReference
* @return
*/
private Object extractObjVal(SoftReference<Cache> cacheSoftReference){
return cacheSoftReference == null ? null : cacheSoftReference.get().getValue();
}
private static class Cache{
private Object value; // 緩存數據對象
private long expiryTime; // 超時時間,以時間戳的形式代表,-1表示不超期
public Cache(Object value, long expiryTime){
this.value = value;
this.expiryTime = expiryTime;
}
/**
* 判斷緩存是否已經超期
* @return true - 已超期,false - 未超期
*/
public boolean isExpiry(){
if(expiryTime == -1){
return false;
}
return System.currentTimeMillis() > expiryTime;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
public static void main(String[] args) throws InterruptedException {
MyCacheManager cacheManager = new MyCacheManager();
cacheManager.put("1", "cobee", 5 * 1000);
cacheManager.put("2", "cgs", 5 * 1000);
cacheManager.put("3", "jone", 5 * 1000);
System.out.println("獲取1號員工的緩存數據:" + cacheManager.get("1"));
Thread.sleep(5000);
System.out.println("等待5秒之後,再次獲取緩存數據");
System.out.println("獲取1號員工的緩存數據:" + cacheManager.get("1"));
}
}