📄 memorystore.java
字号:
package com.easyjf.cache.store;
import com.easyjf.cache.*;
import org.apache.log4j.*;
import java.io.*;
import java.util.*;
/**
*
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public abstract class MemoryStore implements Store {
private final static Logger logger = Logger.getLogger(MemoryStore.class);
/**
*
*/
protected ICache cache;
/**
*
*/
protected Map map;
/**
*
*/
protected int status;
/**
*
* @param cache
* ICache
*/
protected MemoryStore(ICache cache) {
status = Status.STATUS_UNINITIALISED;
this.cache = cache;
status = Status.STATUS_ALIVE;
}
/**
*
*/
protected void clear() {
map.clear();
}
/**
*
* @param key
* Object
* @return boolean
*/
public boolean containsKey(Object key) {
return map.containsKey(key);
}
/**
*
* @param cache
* ICache
* @return MemoryStore
*/
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 dispose() {
if (status == Status.STATUS_SHUTDOWN) {
return;
}
status = Status.STATUS_SHUTDOWN;
// release reference to cache
cache = null;
}
/**
*
* @param element
* Element
*/
protected void doGetOnFoundElement(Element element) {
// nothing for this class
}
protected void doPut(Element element) {
}
/**
*
* @param element
* Element
*/
public synchronized void put(Element element) {
if (element != null) {
map.put(element.getKey(), element);
doPut(element);
}
}
/**
*
* @param key
* Object
* @return Element
*/
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:缓存中没有" + key + ",删除失败。");
return null;
}
}
/**
*
*/
public synchronized void removeAll() {
clear();
}
/**
*
* @param value
* Object
* @return boolean
* @throws IOException
*/
public synchronized boolean removeElement(Object value) throws IOException {
boolean ret=true;
while(map.values().contains(value))
ret&=map.values().remove(value);
return ret;
}
// ~--- get methods --------------------------------------------------------
/**
*
* @param key
* Object
* @return 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;
}
/**
*
* @return Object[]
*/
public synchronized Object[] getKeyArray() {
return map.keySet().toArray();
}
/**
*
* @return Set
*/
public Set getKeySet() {
// TODO Auto-generated method stub
return cache.getKeySet();
}
/**
*
* @param key
* Serializable
* @return Element
*/
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 int getSize() {
return map.size();
}
/**
*
* @return long
* @throws CacheException
*/
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;
}
/**
*
* @return int
*/
public int getStatus() {
return status;
}
/**
*
* @return boolean
*/
protected boolean isFull() {
return map.size() > cache.getMaxElemnetCount();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -