sun.security.util
Class Cache

java.lang.Object
  extended by sun.security.util.Cache

public abstract class Cache
extends Object

Abstract base class and factory for caches. A cache is a key-value mapping. It has properties that make it more suitable for caching than a Map. The factory methods can be used to obtain two different implementations. They have the following properties: . keys and values reside in memory . keys and values must be non-null . maximum size. Replacements are made in LRU order. . optional lifetime, specified in seconds. . save for concurrent use by multiple threads . values are held by either standard references or via SoftReferences. SoftReferences have the advantage that they are automatically cleared by the garbage collector in response to memory demand. This makes it possible to simple set the maximum size to a very large value and let the GC automatically size the cache dynamically depending on the amount of available memory. However, note that because of the way SoftReferences are implemented in HotSpot at the moment, this may not work perfectly as it clears them fairly eagerly. Performance may be improved if the Java heap size is set to larger value using e.g. java -ms64M -mx128M foo.Test Cache sizing: the memory cache is implemented on top of a LinkedHashMap. In its current implementation, the number of buckets (NOT entries) in (Linked)HashMaps is always a power of two. It is recommended to set the maximum cache size to value that uses those buckets fully. For example, if a cache with somewhere between 500 and 1000 entries is desired, a maximum size of 750 would be a good choice: try 1024 buckets, with a load factor of 0.75f, the number of entries can be calculated as buckets / 4 * 3. As mentioned above, with a SoftReference cache, it is generally reasonable to set the size to a fairly large value.


Nested Class Summary
static class Cache.EqualByteArray
          Utility class that wraps a byte array and implements the equals() and hashCode() contract in a way suitable for Maps and caches.
 
Constructor Summary
protected Cache()
           
 
Method Summary
abstract  void clear()
          Remove all entries from the cache.
abstract  Object get(Object key)
          Get a value from the cache.
static Cache newHardMemoryCache(int size)
          Return a new memory cache with the specified maximum size, unlimited lifetime for entries, with the values held by standard references.
static Cache newHardMemoryCache(int size, int timeout)
          Return a new memory cache with the specified maximum size, the specified maximum lifetime (in seconds), with the values held by standard references.
static Cache newNullCache()
          Return a dummy cache that does nothing.
static Cache newSoftMemoryCache(int size)
          Return a new memory cache with the specified maximum size, unlimited lifetime for entries, with the values held by SoftReferences.
static Cache newSoftMemoryCache(int size, int timeout)
          Return a new memory cache with the specified maximum size, the specified maximum lifetime (in seconds), with the values held by SoftReferences.
abstract  void put(Object key, Object value)
          Add an entry to the cache.
abstract  void remove(Object key)
          Remove an entry from the cache.
abstract  int size()
          Return the number of currently valid entries in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

protected Cache()
Method Detail

size

public abstract int size()
Return the number of currently valid entries in the cache.


clear

public abstract void clear()
Remove all entries from the cache.


put

public abstract void put(Object key,
                         Object value)
Add an entry to the cache.


get

public abstract Object get(Object key)
Get a value from the cache.


remove

public abstract void remove(Object key)
Remove an entry from the cache.


newSoftMemoryCache

public static Cache newSoftMemoryCache(int size)
Return a new memory cache with the specified maximum size, unlimited lifetime for entries, with the values held by SoftReferences.


newSoftMemoryCache

public static Cache newSoftMemoryCache(int size,
                                       int timeout)
Return a new memory cache with the specified maximum size, the specified maximum lifetime (in seconds), with the values held by SoftReferences.


newHardMemoryCache

public static Cache newHardMemoryCache(int size)
Return a new memory cache with the specified maximum size, unlimited lifetime for entries, with the values held by standard references.


newNullCache

public static Cache newNullCache()
Return a dummy cache that does nothing.


newHardMemoryCache

public static Cache newHardMemoryCache(int size,
                                       int timeout)
Return a new memory cache with the specified maximum size, the specified maximum lifetime (in seconds), with the values held by standard references.