All Implemented Interfaces:
Serializable, Cloneable, IdentityMap

public class CacheIdentityMap extends FullIdentityMap

Purpose: A fixed size LRU cache

Using a linked list as well as the map from the superclass a LRU cache is maintained. When a get is executed the LRU list is updated and when a new object is inserted the object at the start of the list is deleted (provided the maxSize has been reached).

Responsibilities:

  • Guarantees identity through primary key values
  • Keeps the LRU linked list updated.
Since:
TOPLink/Java 1.0
See Also:
  • Field Details

    • first

      protected LinkedCacheKey first
      Provide handles on the linked list
    • last

      protected LinkedCacheKey last
      Provide handles on the linked list
  • Constructor Details

  • Method Details

    • createCacheKey

      public CacheKey createCacheKey(Object primaryKey, Object object, Object writeLockValue, long readTime)
      Description copied from class: AbstractIdentityMap
      Create the correct type of CacheKey for this map.
      Overrides:
      createCacheKey in class AbstractIdentityMap
    • ensureFixedSize

      protected void ensureFixedSize()
      Reduces the size of the receiver down to the maxSize removing objects from the start of the linked list.
    • getCacheKey

      public CacheKey getCacheKey(Object primaryKeys, boolean forMerge)
      Access the object within the table for the given primaryKey. Move the accessed key to the top of the order keys linked list to maintain LRU.
      Specified by:
      getCacheKey in interface IdentityMap
      Overrides:
      getCacheKey in class FullIdentityMap
      Parameters:
      primaryKeys - is the primary key for the object to search for.
      Returns:
      the LinkedCacheKey or null if none found for primaryKey
    • insertLink

      protected LinkedCacheKey insertLink(LinkedCacheKey key)
      Insert a new element into the linked list of LinkedCacheKeys. New elements (Recently Used) are added at the end (last). Callers of this method must synchronize on the start of the list (this.first).
      Returns:
      the added LinkedCacheKey
    • putCacheKeyIfAbsent

      protected CacheKey putCacheKeyIfAbsent(CacheKey searchKey)
      Also insert the link if the cacheKey is put.
      Overrides:
      putCacheKeyIfAbsent in class FullIdentityMap
    • remove

      public Object remove(CacheKey key)
      Remove the LinkedCacheKey from the cache as well as from the linked list.
      Specified by:
      remove in interface IdentityMap
      Overrides:
      remove in class FullIdentityMap
      Returns:
      the LinkedCacheKey to be removed.
    • removeLink

      protected LinkedCacheKey removeLink(LinkedCacheKey key)
      Remove the LinkedCacheKey from the linked list. Callers of this method must synchronize on the start of the list (this.first).
      Returns:
      the removed LinkedCacheKey.
    • updateMaxSize

      public void updateMaxSize(int maxSize)
      INTERNAL: This method will be used to update the max cache size, any objects exceeding the max cache size will be remove from the cache. Please note that this does not remove the object from the identityMap, except in the case of the CacheIdentityMap.
      Specified by:
      updateMaxSize in interface IdentityMap
      Overrides:
      updateMaxSize in class AbstractIdentityMap