📄 fifomemorystore.java
字号:
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 - 2004 Greg Luck. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by Greg Luck
* (http://sourceforge.net/users/gregluck) and contributors.
* See http://sourceforge.net/project/memberlist.php?group_id=93232
* for a list of contributors"
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "EHCache" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact Greg Luck (gregluck at users.sourceforge.net).
*
* 5. Products derived from this software may not be called "EHCache"
* nor may "EHCache" appear in their names without prior written
* permission of Greg Luck.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GREG LUCK OR OTHER
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by contributors
* individuals on behalf of the EHCache project. For more
* information on EHCache, please see <http://ehcache.sourceforge.net/>.
*
*/
package com.easyjf.cache.store;
/**
* 这里直接使用的Apache开源缓存ECache中的算法
*/
import com.easyjf.cache.*;
import org.apache.commons.collections.SequencedHashMap;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
/**
* First-In-First-Out (FIFO) implementation of MemoryStore.
*
* @author <a href="mailto:ssuravarapu@users.sourceforge.net">Surya Suravarapu</a>
* @version $Id: FifoMemoryStore.java,v 1.9 2005/10/15 17:32:30 gregluck Exp $
*/
public class FifoMemoryStore extends MemoryStore {
private final static Logger logger = Logger.getLogger(FifoMemoryStore.class);
private final static int SEQUENCED_HASH_MAP = 1;
private final static int LINKED_HASH_MAP = 2;
/**
* One of the above declared static collection types
*/
private int collectionType;
/**
* Constructor for the FifoMemoryStore object.
* <p/>
* First tries to use {@link java.util.LinkedHashMap}. If not found uses
* Jakarta Commons collections.
*/
public FifoMemoryStore(ICache cache) {
super(cache);
// Use LinkedHashMap for JDK 1.4
try {
Class.forName("java.util.LinkedHashMap");
map = new LinkedHashMap();
collectionType = LINKED_HASH_MAP;
} catch (ClassNotFoundException e) {
// If not JDK 1.4 use the commons collections
try {
Class.forName("org.apache.commons.collections.SequencedHashMap");
map = new SequencedHashMap();
collectionType = SEQUENCED_HASH_MAP;
} catch (ClassNotFoundException ee) {
logger.error(ee.getMessage());
}
}
}
/**
* Allow specialised actions over adding the element to the map
*
* @param element
*/
protected void doPut(Element element) {
if (isFull()) {
removeFirstElement();
}
}
/**
* Returns the first eligible element that can be taken out of the cache
* based on the FIFO policy
*/
public synchronized Element getFirstElement() {
if (map.size() == 0) {
return null;
}
Element element = null;
Serializable key = null;
if (collectionType == LINKED_HASH_MAP) {
Set keySet = map.keySet();
Iterator itr = keySet.iterator();
// The first element is the candidate to remove
if (itr.hasNext()) {
key = (Serializable) itr.next();
element = (Element) map.get(key);
}
} else if (collectionType == SEQUENCED_HASH_MAP) {
key = (Serializable) ((SequencedHashMap) map).getFirstKey();
element = (Element) map.get(key);
}
return element;
}
/**
* Remove the first element that is eligible to removed from the store
* based on the FIFO policy
*/
private void removeFirstElement() {
Element element = getFirstElement();
if (cache.isExpired(element)) {
remove(element.getKey());
return;
}
remove(element.getKey());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -