This implementation uses a "soft limit" to the in-memory size of the cache,
meaning that all cache entries are kept within a completely
java.-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.
org.hibernate.cfg.Environment#QUERY_PLAN_CACHE_MAX_STRONG_REFERENCES , org.hibernate.cfg.Environment#QUERY_PLAN_CACHE_MAX_SOFT_REFERENCES
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | DEFAULT_SOFT_REF_COUNT
The default soft reference count. |
| public static final int | DEFAULT_STRONG_REF_COUNT
The default strong reference count. |
| private transient ReferenceQueue | |
| private transient LRUMap | |
| private final int | |
| private transient LRUMap | |
| private final int |
| Access | Constructor and Description |
|---|---|
| public | |
| public | SoftLimitMRUCache(int
the strong reference count. strongRefCount, int the soft reference count. softRefCount)Constructs a cache with the specified settings. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | |
| private void | |
| public synchronized Object | |
| private void | |
| public synchronized Object | |
| private void | |
| public synchronized int | |
| public synchronized int |
| DEFAULT_SOFT_REF_COUNT | back to summary |
|---|---|
| public static final int DEFAULT_SOFT_REF_COUNT The default soft reference count. | |
| DEFAULT_STRONG_REF_COUNT | back to summary |
|---|---|
| public static final int DEFAULT_STRONG_REF_COUNT The default strong reference count. | |
| referenceQueue | back to summary |
|---|---|
| private transient ReferenceQueue<T> referenceQueue | |
| softRefCache | back to summary |
|---|---|
| private transient LRUMap softRefCache | |
| softRefCount | back to summary |
|---|---|
| private final int softRefCount | |
| strongRefCache | back to summary |
|---|---|
| private transient LRUMap strongRefCache | |
| strongRefCount | back to summary |
|---|---|
| private final int strongRefCount | |
| SoftLimitMRUCache | back to summary |
|---|---|
| public SoftLimitMRUCache() Constructs a cache with the default settings. | |
| SoftLimitMRUCache | back to summary |
|---|---|
| public SoftLimitMRUCache(int strongRefCount, int softRefCount) Constructs a cache with the specified settings.
| |
| clear | back to summary |
|---|---|
| public synchronized void clear() Clears the cache. | |
| clearObsoleteReferences | back to summary |
|---|---|
| private void clearObsoleteReferences() | |
| get | back to summary |
|---|---|
| public synchronized Object get(Object key) Gets an object from the cache. | |
| init | back to summary |
|---|---|
| private void init() | |
| put | back to summary |
|---|---|
| public synchronized Object put(Object key, Object value) Puts a value in the cache. | |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException | |
| size | back to summary |
|---|---|
| public synchronized int size() Gets the strong reference cache size.
| |
| softSize | back to summary |
|---|---|
| public synchronized int softSize() Gets the soft reference cache size.
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private Object |
| key | back to summary |
|---|---|
| private final Object key | |
| KeyedSoftReference | back to summary |
|---|---|
private KeyedSoftReference(Object key, Object value, ReferenceQueue<T> q)
| |
| getKey | back to summary |
|---|---|
| private Object getKey() | |