## org.hibernate.util
Class SoftLimitMRUCache

    java.lang.Object
      org.hibernate.util.SoftLimitMRUCache

All Implemented Interfaces:
  Serializable

    public class SoftLimitMRUCache

extends Object
implements Serializable

Cache following a "Most Recently Used" (MRU) 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 `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.

The soft-size is bounded and configurable. This allows controlling memory usage which can grow out of control under some circumstances, especially when very large heaps are in use. Although memory usage per se should not be a problem with soft references, which are cleared when necessary, this can trigger extremely slow stop-the-world GC pauses when nearing full heap usage, even with CMS concurrent GC (i.e. concurrent mode failure). This is most evident when ad-hoc HQL queries are produced by the application, leading to poor soft-cache hit ratios. This can also occur with heavy use of SQL IN clauses, which will generate multiples SQL queries (even if parameterized), one for each collection/array size passed to the IN clause. Many slightly different queries will eventually fill the heap and trigger a full GC to reclaim space, leading to unacceptable pauses in some cases.

Note: This class is serializable, however all entries are discarded on serialization.

Author:
  Steve Ebersole, Manuel Dominguez Sarmiento
See Also:
  `Environment.QUERY_PLAN_CACHE_MAX_STRONG_REFERENCES`, `Environment.QUERY_PLAN_CACHE_MAX_SOFT_REFERENCES`, Serialized Form

Field Summary
`static int` `DEFAULT_SOFT_REF_COUNT` The default soft reference count.
`static int` `DEFAULT_STRONG_REF_COUNT` The default strong reference count.

Constructor Summary
`SoftLimitMRUCache()` Constructs a cache with the default settings.
`SoftLimitMRUCache(int strongRefCount, int softRefCount)` Constructs a cache with the specified settings.

Method Summary
`void` `clear()` Clears the cache.
`Object` `get(Object key)` Gets an object from the cache.
`Object` `put(Object key, Object value)` Puts a value in the cache.
`int` `size()` Gets the strong reference cache size.
`int` `softSize()` Gets the soft reference cache size.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DEFAULT_STRONG_REF_COUNT

    public static final int DEFAULT_STRONG_REF_COUNT

  The default strong reference count.

  See Also:
    Constant Field Values

### DEFAULT_SOFT_REF_COUNT

    public static final int DEFAULT_SOFT_REF_COUNT

  The default soft reference count.

  See Also:
    Constant Field Values

Constructor Detail

### SoftLimitMRUCache

    public SoftLimitMRUCache()

  Constructs a cache with the default settings.

  See Also:
    `DEFAULT_STRONG_REF_COUNT`, `DEFAULT_SOFT_REF_COUNT`

### SoftLimitMRUCache

    public SoftLimitMRUCache(int strongRefCount,
                             int softRefCount)

  Constructs a cache with the specified settings.

  Parameters:
    `strongRefCount` - the strong reference count.
    `softRefCount` - the soft reference count.
  Throws:
    `IllegalArgumentException` - if either of the arguments is less than one, or if the strong reference count is higher than the soft reference count.

Method Detail

### get

    public Object get(Object key)

  Gets an object from the cache.

  Parameters:
    `key` - the cache key.
  Returns:
    the stored value, or `null` if no entry exists.

### put

    public Object put(Object key,
                      Object value)

  Puts a value in the cache.

  Parameters:
    `key` - the key.
    `value` - the value.
  Returns:
    the previous value stored in the cache, if any.

### size

    public int size()

  Gets the strong reference cache size.

  Returns:
    the strong reference cache size.

### softSize

    public int softSize()

  Gets the soft reference cache size.

  Returns:
    the soft reference cache size.

### clear

    public void clear()

  Clears the cache.

Copyright © 2001-2010 Red Hat, Inc. All Rights Reserved.