Class DBObjectCache<T extends DatabaseObject>


  • public class DBObjectCache<T extends DatabaseObject>
    extends java.lang.Object
    Generic cache implementation for objects that extend DatabaseObject.
    • Constructor Summary

      Constructors 
      Constructor Description
      DBObjectCache​(int hardCacheSize)
      Constructs a new DBObjectCache with a given hard cache size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void delete​(long key)
      Removes the object with the given key from the cache.
      void delete​(java.util.List<KeyRange> keyRanges)
      Delete all objects from hashTable whose key is contained within the specified keyRanges.
      T get​(long key)
      Retrieves the database object with the given key from the cache.
      T get​(Record objectRecord)
      Retrieves the database object with the given record and associated key from the cache.
      java.util.ArrayList<T> getCachedObjects()
      Returns an array of all the cached objects.
      void invalidate()
      Marks all the cached objects as invalid.
      void invalidate​(long key)
      Invalidates the object with given key.
      void invalidate​(long startKey, long endKey)
      Invalidates a range of objects in the cache.
      void keyChanged​(long oldKey, long newKey)  
      void setHardCacheSize​(int size)
      Sets the number of objects to protect against garbage collection.
      int size()
      Returns the number of objects currently in the cache.
      • Methods inherited from class java.lang.Object

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

      • DBObjectCache

        public DBObjectCache​(int hardCacheSize)
        Constructs a new DBObjectCache with a given hard cache size. The hard cache size is the minimum number of objects to keep in the cache. Typically, the cache will contain more than this number, but the excess objects are subject to garbage collections
        Parameters:
        hardCacheSize - the minimum number of objects to keep in the cache.
    • Method Detail

      • get

        public T get​(long key)
        Retrieves the database object with the given key from the cache.
        Parameters:
        key - the key of the object to retrieve.
        Returns:
        the cached object or null if the object with that key is not currently cached.
      • get

        public T get​(Record objectRecord)
        Retrieves the database object with the given record and associated key from the cache. This form should be used in conjunction with record iterators to avoid unnecessary record query during a possible object refresh. To benefit from the record the cached object must implement the DatabaseObject.refresh(Record) method which by default ignores the record and simply calls DatabaseObject.refresh().
        Parameters:
        record - the valid record corresponding to the object to be retrieved and possibly used to refresh the associated object if found in cache
        Returns:
        the cached object or null if the object with that key is not currently cached.
      • size

        public int size()
        Returns the number of objects currently in the cache.
      • setHardCacheSize

        public void setHardCacheSize​(int size)
        Sets the number of objects to protect against garbage collection.
        Parameters:
        size - the minimum number of objects to keep in the cache.
      • getCachedObjects

        public java.util.ArrayList<T> getCachedObjects()
        Returns an array of all the cached objects.
      • delete

        public void delete​(java.util.List<KeyRange> keyRanges)
        Delete all objects from hashTable whose key is contained within the specified keyRanges.
        Parameters:
        keyRanges - key ranges to delete
      • invalidate

        public void invalidate()
        Marks all the cached objects as invalid. Invalid objects will have to refresh themselves before they are allowed to be used. If an invalidated object cannot refresh itself, then the object is removed from the cache and discarded and the application can no longer use that instance of the object.
      • invalidate

        public void invalidate​(long startKey,
                               long endKey)
        Invalidates a range of objects in the cache.
        Parameters:
        startKey - the first key in the range to invalidate.
        endKey - the last key in the range to invalidate.
      • delete

        public void delete​(long key)
        Removes the object with the given key from the cache.
        Parameters:
        key - the key of the object to remove.
      • invalidate

        public void invalidate​(long key)
        Invalidates the object with given key.
        Parameters:
        key - the key of the object to invalidate.
      • keyChanged

        public void keyChanged​(long oldKey,
                               long newKey)