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

📄 lrusoftcache.java

📁 老外的在线考试
💻 JAVA
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. *  * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA  02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.util;import java.util.*;import java.lang.ref.SoftReference;/*** LRUSoftCache is a LRU cache of key-value pairs.* It works the same as LRUCache, except that when an object is removed from its* main cache, it is kept in a secondary cache with a soft reference.* It is reclaimed by the garbage collector when the JVM needs more memory.* However, if the softly referenced object is accessed before it is* garbage collected, it is placed back into the main cache.** @author Alexander Yap* @version $Revision: 1.2 $ at $Date: 2004/02/18 08:35:17 $ by $Author: alexycyap $*/public class LRUSoftCache extends LRUCache{	/**	* Creates a LRUSoftCache with main cache max capacity of 64 and initial capacity of 32.	*/	public LRUSoftCache()	{		this(32, 64);	}	/**	* Creates a LRUSoftCache.	* @param initCapacity Main cache initial capacity.	* @param maxCapacity Maximum number of key-value pairs that can be stored in the main cache.	*/	public LRUSoftCache(int initCapacity, int maxCapacity)	{		super(initCapacity, maxCapacity);		// The secondary soft-reference cache is initialized to twice the size of the main cache.		m_softRefCache = new HashMap(initCapacity*2);	}	public Object getFromCache(Object key)	{		Object retObj = super.get(key);		if (retObj==null)		{			SoftReference ref = (SoftReference)m_softRefCache.get(key);			if (ref!=null)			{				retObj = ref.get();				// If object has been accessed from the soft cache, move it to the main cache				if (retObj!=null)				{					super.put( key, retObj );				}			}		}		return retObj;	}	public void clearFromCache()	{		super.clear();		m_softRefCache.clear();	}	public boolean containsKeyInCache(Object key)	{		return ( super.containsKey(key) || m_softRefCache.containsKey(key));	}	public boolean containsValueInCache(Object value)	{		return ( super.containsValue(value) || m_softRefCache.containsValue(value));	}	public Object removeFromCache(Object key)	{		Object obj = super.remove(key);		Object obj2 = m_softRefCache.remove(key);		if (obj==null)		{			return obj2;		}		return obj;	}	public boolean isCacheEmpty()	{		return (super.isEmpty() && m_softRefCache.isEmpty());	}	protected boolean removeEldestEntry(Map.Entry eldest)	{		Object key = eldest.getKey();		// MappedSoftCache handles it own removal, so this method always returns false.		// It moves the removed object to the secondary cache.		if ( super.size()>m_maxSize )		{			Object objRemoved = super.remove(key);			// if an object with the same key already exists in the soft cache,			// clear it first.			SoftReference ref = (SoftReference)m_softRefCache.get(key);			if (ref!=null)			{				ref.clear();			}			m_softRefCache.put(key, new SoftReference(objRemoved));		}		return false;	}	// Secondary soft reference cache	private Map m_softRefCache = null;}

⌨️ 快捷键说明

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