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

📄 softlimitmrucache.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
字号:
package org.hibernate.util;

import org.apache.commons.collections.ReferenceMap;
import org.apache.commons.collections.LRUMap;

import java.io.Serializable;
import java.io.IOException;

/**
 * Cache following a "Most Recently Used" (MRY) algorithm for maintaining a
 * bounded in-memory size; the "Least Recently Used" (LRU) entry is the first
 * available for removal from the cache.
 * <p/>
 * This implementation uses a "soft limit" to the in-memory size of the cache,
 * meaning that all cache entries are kept within a completely
 * {@link java.lang.ref.SoftReference}-based map with the most recently utilized
 * entries additionally kept in a hard-reference manner to prevent those cache
 * entries soft references from becoming enqueued by the garbage collector.
 * Thus the actual size of this cache impl can actually grow beyond the stated
 * max size bound as long as GC is not actively seeking soft references for
 * enqueuement.
 *
 * @author Steve Ebersole
 */
public class SoftLimitMRUCache implements Serializable {

	public static final int DEFAULT_STRONG_REF_COUNT = 128;

	private final int strongReferenceCount;

	// actual cache of the entries.  soft references are used for both the keys and the
	// values here since the values pertaining to the MRU entries are kept in a
	// seperate hard reference cache (to avoid their enqueuement/garbage-collection).
	private transient ReferenceMap softReferenceCache = new ReferenceMap( ReferenceMap.SOFT, ReferenceMap.SOFT );
	// the MRU cache used to keep hard references to the most recently used query plans;
	// note : LRU here is a bit of a misnomer, it indicates that LRU entries are removed, the
	// actual kept entries are the MRU entries
	private transient LRUMap strongReferenceCache;

	public SoftLimitMRUCache() {
		this( DEFAULT_STRONG_REF_COUNT );
	}

	public SoftLimitMRUCache(int strongRefCount) {
		this.strongReferenceCount = strongRefCount;
		init();
	}

	public synchronized Object get(Object key) {
		Object result = softReferenceCache.get( key );
		if ( result != null ) {
			strongReferenceCache.put( key, result );
		}
		return result;
	}

	public synchronized Object put(Object key, Object value) {
		softReferenceCache.put( key, value );
		return strongReferenceCache.put( key, value );
	}

	public synchronized int size() {
		return strongReferenceCache.size();
	}

	public synchronized int softSize() {
		return softReferenceCache.size();
	}

	private void init() {
		strongReferenceCache = new LRUMap( strongReferenceCount );
	}

	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
		init();
	}

	public synchronized void clear() {
		strongReferenceCache.clear();
		softReferenceCache.clear();
	}
}

⌨️ 快捷键说明

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