FileDocCategorySizeDatePackage
SoftLimitMRUCache.javaAPI DocHibernate 3.2.52765Wed Feb 15 09:35:34 GMT 2006org.hibernate.util

SoftLimitMRUCache

public class SoftLimitMRUCache extends Object implements Serializable
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.

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

Fields Summary
public static final int
DEFAULT_STRONG_REF_COUNT
private final int
strongReferenceCount
private transient org.apache.commons.collections.ReferenceMap
softReferenceCache
private transient org.apache.commons.collections.LRUMap
strongReferenceCache
Constructors Summary
public SoftLimitMRUCache()


	  
		this( DEFAULT_STRONG_REF_COUNT );
	
public SoftLimitMRUCache(int strongRefCount)

		this.strongReferenceCount = strongRefCount;
		init();
	
Methods Summary
public synchronized voidclear()

		strongReferenceCache.clear();
		softReferenceCache.clear();
	
public synchronized java.lang.Objectget(java.lang.Object key)

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

		strongReferenceCache = new LRUMap( strongReferenceCount );
	
public synchronized java.lang.Objectput(java.lang.Object key, java.lang.Object value)

		softReferenceCache.put( key, value );
		return strongReferenceCache.put( key, value );
	
private voidreadObject(java.io.ObjectInputStream in)

		in.defaultReadObject();
		init();
	
public synchronized intsize()

		return strongReferenceCache.size();
	
public synchronized intsoftSize()

		return softReferenceCache.size();