?? memorystore.java
字號(hào):
package com.easyjf.cache.store;
import com.easyjf.cache.*;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public abstract class MemoryStore implements Store {
private final static Logger logger = Logger.getLogger(MemoryStore.class);
protected ICache cache;
protected Map map;
protected int status;
protected MemoryStore(ICache cache) {
status = Status.STATUS_UNINITIALISED;
this.cache = cache;
status = Status.STATUS_ALIVE;
}
public static MemoryStore create(ICache cache) {
MemoryStore memoryStore = null;
switch(cache.getStorePolicy())
{
case(StorePolicy.LRU):
memoryStore=new LruMemoryStore(cache);
break;
case(StorePolicy.FIFO):
memoryStore=new FifoMemoryStore(cache);
break;
case (StorePolicy.LFU):
memoryStore=new LfuMemoryStore(cache);
}
return memoryStore;
}
public synchronized void put(Element element) {
if (element != null) {
map.put(element.getKey(), element);
doPut(element);
}
}
protected void doPut(Element element) {
}
public synchronized Element get(Object key) {
Element element = (Element) map.get(key);
if (element != null) {
element.updateAccessStatistics();
doGetOnFoundElement(element);
logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore中找到" + key);
} else logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找不到" + key);
return element;
}
protected void doGetOnFoundElement(Element element) {
//nothing for this class
}
public synchronized Element getQuiet(Serializable key) {
Element cacheElement = (Element) map.get(key);
if (cacheElement != null) {
logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找到" + key);
} else
logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找不到" + key);
return cacheElement;
}
public synchronized Element remove(Object key) {
// remove single item.
Element element = (Element) map.remove(key);
if (element != null) {
return element;
} else {
logger.debug(cache.getName() + "Cache:緩存中沒(méi)有" + key + ",刪除失敗。");
return null;
}
}
public synchronized void removeAll() {
clear();
}
protected void clear() {
map.clear();
}
public synchronized void dispose() {
if (status==Status.STATUS_SHUTDOWN) {
return;
}
status = Status.STATUS_SHUTDOWN;
//release reference to cache
cache = null;
}
public int getStatus() {
return status;
}
public synchronized Object[] getKeyArray() {
return map.keySet().toArray();
}
public int getSize() {
return map.size();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public synchronized long getSizeInBytes() throws CacheException {
long sizeInBytes = 0;
for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
Element element = (Element) iterator.next();
if (element != null) {
sizeInBytes += element.getSerializedSize();
}
}
return sizeInBytes;
}
protected boolean isFull() {
return map.size() > cache.getMaxElemnetCount();
}
public Set getKeySet() {
// TODO Auto-generated method stub
return cache.getKeySet();
}
public boolean removeElement(Object value) throws IOException {
// TODO Auto-generated method stub
return map.values().remove(value);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -