FileDocCategorySizeDatePackage
HardCacheWeakIdentityMap.javaAPI DocGlassfish v2 API7281Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.identitymaps

HardCacheWeakIdentityMap

public class HardCacheWeakIdentityMap extends WeakIdentityMap

Purpose: A weak cache is identical to the weak identity map, however the weak 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 weak cache solves this through also holding a fixed number of objects is memory to improve caching. This makes used 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 through maintaining a fixed size cache of MRU objects when memory is available
  • The default size of the reference cache is half the max size
since
TOPLink/Java 1.2

Fields Summary
protected ExposedNodeLinkedList
referenceCache
Constructors Summary
public HardCacheWeakIdentityMap(int size)

        super(size);
        this.referenceCache = new ExposedNodeLinkedList();
    
Methods Summary
public java.lang.ObjectbuildReference(java.lang.Object object)
Creates a Soft reference if Required

param
object is the domain object to cache.

        return object;
    
public oracle.toplink.essentials.internal.identitymaps.CacheKeycreateCacheKey(java.util.Vector primaryKey, java.lang.Object object, java.lang.Object writeLockValue, long readTime)
Use a ReferenceCacheKey that also stores the linked list node to manage the LRU sub-cache of references.

        return new ReferenceCacheKey(primaryKey, object, writeLockValue, readTime);
    
public oracle.toplink.essentials.internal.helper.linkedlist.ExposedNodeLinkedListgetReferenceCache()
Return the linked reference cache.

        return referenceCache;
    
public booleanhasReference(java.lang.Object reference)
Checks if the object is null, or reference's object is null.

param
the object for hard or the reference for soft.

        return reference != null;
    
protected voidput(oracle.toplink.essentials.internal.identitymaps.CacheKey cacheKey)
Store the object in the cache with the cache key. Also store the linked list node in the cache key.

        ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey)cacheKey;
        LinkedNode node = getReferenceCache().addFirst(buildReference(referenceCacheKey.getObject()));
        referenceCacheKey.setReferenceCacheNode(node);
        super.put(cacheKey);
    
public java.lang.Objectremove(oracle.toplink.essentials.internal.identitymaps.CacheKey cacheKey)
Remove the cache key from the map and the sub-cache list.

        if (cacheKey == null) {
            return null;
        }
        ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey)cacheKey;
        synchronized (this){
            getReferenceCache().remove(referenceCacheKey.getReferenceCacheNode());
        }
        return super.remove(cacheKey);
    
public synchronized voidupdateMaxSize(int maxSize)
This method will be used to update the max cache size.

        setMaxSize(maxSize);
        // Remove the LRU items if max size exceeded.
        while (getReferenceCache().size() > getMaxSize()) {
            getReferenceCache().removeLast();
        }