package com.alex.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class MapCache {
//缓存的size
private int canCacheSize;
//超时时间
private long timeOut;
//map
private Map<Object,Object> cache;
//
private long lastestClear;
//map
private Map<Object,CacheEntryInfo> cacheInfo;
//list
private static List<MapCache> usedCache = new ArrayList<MapCache>();
/**
*
* @param cacheSize h缓存大小
* @param timeOut 过期时间
*/
public MapCache(int cacheSize,int timeOut){
this.canCacheSize = cacheSize;
//毫秒计算
this.timeOut = timeOut*1000;
//初使化
if(cacheSize>0){
cache = new HashMap<Object,Object>(cacheSize);
}else{
cache = new HashMap<Object,Object>();
}
//最近操作时间
if(timeOut >0 ){
lastestClear = System.currentTimeMillis();
}
if(cacheSize>0 || timeOut > 0){
cacheInfo = new HashMap<Object,CacheEntryInfo>(cacheSize>0?cacheSize:10);
}
usedCache.add(this);
}
/**
* 清除所有
*/
public static void clearAllCache(){
for(int i=0; i<usedCache.size(); i++){
((MapCache)usedCache.get(i)).clearCache();
}
}
/**
* 清除
*/
public synchronized void clearCache(){
if(cacheInfo != null){
cacheInfo.clear();
}
cache.clear();
lastestClear = System.currentTimeMillis();
}
/**
* 存放
* @param key
* @param value
*/
public synchronized void putToCache(Object key, Object value){
if(timeOut>0 && lastestClear + timeOut < System.currentTimeMillis()){
cleanTimeOutCache();
}
if(canCacheSize>0 && cache.size()>canCacheSize && cacheInfo.get(key)!= null){
removeOldestEntry();
}
if(cacheInfo != null){
cacheInfo.put(key, new CacheEntryInfo(key));
}
cache.put(key, value);
}
public Object getFromCache(Object key){
if(timeOut>0 && lastestClear + timeOut < System.currentTimeMillis()){
clearTimeOutCache();
}
return cache.get(key);
}
/**
*
* @return
*/
public Collection<Object> listAllCacheItems(){
if(timeOut>0 && lastestClear + timeOut < System.currentTimeMillis()){
clearTimeOutCache();
}
return cache.values();
}
/**
*
* @param key
* @return
*/
public synchronized Object removeFromCache(Object key){
if(timeOut >0 && lastestClear + timeOut < System.currentTimeMillis()){
clearTimeOutCache();
}
if(cacheInfo != null){
cacheInfo.remove(key);
}
return cache.remove(key);
}
/**
* 获取缓存大小
* @return
*/
public int getCachedSize(){
if(timeOut>0 && timeOut+lastestClear<System.currentTimeMillis()){
clearTimeOutCache();
}
return cache.size();
}
private synchronized void clearTimeOutCache() {
lastestClear = System.currentTimeMillis();
List<Object> removableKey = new ArrayList<Object>(10);
CacheEntryInfo cacheEntryInfo;
for(Iterator<CacheEntryInfo> it = cacheInfo.values().iterator();
it.hasNext();){
cacheEntryInfo = it.next();
if(cacheEntryInfo == null){
continue;
}
if(cacheEntryInfo.lastUsedTime +timeOut< lastestClear){
removableKey.add(cacheEntryInfo.key);
}
}
for(int i=0; i<removableKey.size(); i++){
cacheInfo.remove(removableKey.get(i));
cache.remove(removableKey.get(i));
}
}
/**
* 清除
*/
private synchronized void removeOldestEntry() {
CacheEntryInfo tmp, oldest = null;
for(Iterator<CacheEntryInfo> it = cacheInfo.values().iterator();
it.hasNext();){
tmp = it.next();
if(tmp == null){
continue;
}
if(oldest == null || oldest.lastUsedTime > tmp.lastUsedTime){
oldest = tmp;
}
if(oldest != null){
cacheInfo.remove(oldest.key);
cache.remove(oldest.key);
}
}
}
/**
* 清除
*/
private synchronized void cleanTimeOutCache() {
lastestClear = System.currentTimeMillis();
List<Object> removableKey = new ArrayList<Object>(10);
CacheEntryInfo cacheEntryInfo;
for(Iterator<CacheEntryInfo> it = cacheInfo.values().iterator();
it.hasNext();){
cacheEntryInfo = it.next();
if(cacheEntryInfo == null){
continue;
}
if(cacheEntryInfo.lastUsedTime + timeOut < lastestClear){
removableKey.add(cacheEntryInfo.key);
}
}
for(int i=0; i<removableKey.size(); i++){
cacheInfo.remove(removableKey.get(i));
cache.remove(removableKey.get(i));
}
}
public static void main(String[] args) {
int cacheSize = 10;
int timeOut = 1;
MapCache mc = new MapCache(cacheSize,timeOut);
mc.putToCache("", "");
}
}