Class HardCacheWeakIdentityMap

All Implemented Interfaces:
Serializable, Cloneable, IdentityMap
Direct Known Subclasses:
SoftCacheWeakIdentityMap

public class HardCacheWeakIdentityMap extends WeakIdentityMap

Purpose: A HardCacheWeakIdentityMap is identical to the weak identity map, however the weak reference can be a performance problem for some types of apps because it can cause too much garbage collection of objects read causing them to be re-read and re-built (this defeats the purpose of the cache). The hard weak cache solves this by also holding a fixed number of objects in memory to improve caching.
This class makes use of an exposed node linked list to maintain the objects by storing the link nodes in the cache key.

Responsibilities:

  • Guarantees identity
  • Allows garbage collection
  • Increases performance by maintaining a fixed size cache of LRU objects when memory is available
  • The default size of the reference cache is half the max size
Since:
TOPLink/Java 1.2
See Also:
  • Field Details

    • referenceCache

      protected ExposedNodeLinkedList referenceCache
      A subset of cache entries have hard references maintained in this list to reduce garbage collection frequency
  • Constructor Details

  • Method Details

    • createCacheKey

      public CacheKey createCacheKey(Object primaryKey, Object object, Object writeLockValue, long readTime)
      Use a ReferenceCacheKey that also stores the linked list node to manage the LRU sub-cache of references.
      Overrides:
      createCacheKey in class WeakIdentityMap
    • getReferenceCache

      public ExposedNodeLinkedList getReferenceCache()
      Return the linked reference cache.
    • buildReference

      public Object buildReference(Object object)
      Allows subclass to create a SoftReference to the object.
      Parameters:
      object - is the domain object to cache.
    • hasReference

      public boolean hasReference(Object reference)
      Checks if the object is null, or reference's object is null.
      Parameters:
      reference - the object for hard or the reference for soft.
    • remove

      public Object remove(CacheKey cacheKey)
      Remove the cache key from the map and the sub-cache list.
      Specified by:
      remove in interface IdentityMap
      Overrides:
      remove in class FullIdentityMap
      Returns:
      the object held within the CacheKey or null if no object cached for given cacheKey.
    • put

      public CacheKey put(Object primaryKey, Object object, Object writeLockValue, long readTime)
      Store the object in the cache at its primary key, and add to sub-cache list.
      Specified by:
      put in interface IdentityMap
      Overrides:
      put in class FullIdentityMap
      Parameters:
      primaryKey - is the primary key for the object.
      object - is the domain object to cache.
      writeLockValue - is the current write lock value of object, if null the version is ignored.
    • updateMaxSize

      public void updateMaxSize(int maxSize)
      This method will be used to update the max cache size.
      Specified by:
      updateMaxSize in interface IdentityMap
      Overrides:
      updateMaxSize in class AbstractIdentityMap