⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fifomemorystore.java

📁  EasyDBO是一个超轻量级对象-关系映射(Object/Relation Mapping
💻 JAVA
字号:
package com.easyjf.cache.store;

//~--- non-JDK imports --------------------------------------------------------

import com.easyjf.cache.*;

import org.apache.commons.collections.*;
import org.apache.log4j.*;

//~--- JDK imports ------------------------------------------------------------

/**
 * 这里直接使用的Apache开源缓存ECache中的算法
 */
import java.io.*;

import java.util.*;

// ~--- classes ----------------------------------------------------------------

/**
 * 
 * <p>
 * Title: 先进先出的存储算法
 * </p>
 * 
 * <p>
 * Description: 该算法直接借鉴Apache EhCache的缓存算法
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company:EasyJF开源团队-EasyDBO项目组
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
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;

	// ~--- fields -------------------------------------------------------------

	/**
	 * One of the above declared static collection types
	 */
	private int collectionType;

	// ~--- constructors -------------------------------------------------------

	/**
	 * 
	 * @param cache
	 *            ICache
	 */
	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());
			}
		}
	}

	/**
	 * 
	 * @param element
	 *            Element
	 */
	protected void doPut(Element element) {		
		if (isFull()) {
			removeFirstElement();
		}
	}

	/**
	 * 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());
	}

	// ~--- get methods --------------------------------------------------------

	/**
	 * 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;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -