Class ObjectCache


  • public class ObjectCache
    extends java.lang.Object
    ObjectClass provides a fixed-size long-key-based object cache. Both a hard and weak cache are maintained, where the weak cache is only limited by available memory. This cache mechanism is useful in ensuring that only a single object instance for a given key exists.

    The weak cache is keyed, while the hard cache simply maintains the presence of an object in the weak cache.

    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectCache​(int hardCacheSize)
      Construct a keyed-object cache of size hardCacheSize.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear both hard and weak caches.
      boolean contains​(long key)
      Determine if the keyed-object exists in the cache.
      java.lang.Object get​(long key)
      Get the object from cache which corresponds to the specified key.
      void put​(long key, java.lang.Object obj)
      Add an object to the cache
      void remove​(long key)
      Remove the specified keyed object from both hard and weak caches.
      void remove​(long startKey, long endKey)
      Remove the specified range of keyed objects from both hard and weak caches.
      void setHardCacheSize​(int size)
      Adjust the hard cache size
      int size()
      Return the hard cache size
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ObjectCache

        public ObjectCache​(int hardCacheSize)
        Construct a keyed-object cache of size hardCacheSize.
        Parameters:
        hardCacheSize - hard cache size.
    • Method Detail

      • contains

        public boolean contains​(long key)
        Determine if the keyed-object exists in the cache.
        Parameters:
        key - object key
        Returns:
        true if object is cached
      • get

        public java.lang.Object get​(long key)
        Get the object from cache which corresponds to the specified key.
        Parameters:
        key - object key
        Returns:
        cached object
      • size

        public int size()
        Return the hard cache size
      • setHardCacheSize

        public void setHardCacheSize​(int size)
        Adjust the hard cache size
        Parameters:
        size - new hard cache size
      • put

        public void put​(long key,
                        java.lang.Object obj)
        Add an object to the cache
        Parameters:
        key - object key
        obj - the object
      • clear

        public void clear()
        Clear both hard and weak caches. The cache should be cleared when all cached objects have become invalid.
      • remove

        public void remove​(long startKey,
                           long endKey)
        Remove the specified range of keyed objects from both hard and weak caches. A cache range should be cleared when the corresponding objects have become invalid.
        Parameters:
        startKey - minimum object key value
        endKey - maximum object key value
      • remove

        public void remove​(long key)
        Remove the specified keyed object from both hard and weak caches. An object should be removed from the cache when it becomes invalid.
        Parameters:
        key - object key