Class DBObjectCache<T extends DatabaseObject>

  • Type Parameters:
    T - The type of the object stored in this cache

    public class DBObjectCache<T extends DatabaseObject>
    extends java.lang.Object
    Generic cache implementation for objects that extend DatabaseObject. This is a reference based cache such that objects are only ever automatically removed from the cache when there are no references to that object. It also maintains a small "hard" cache so that recently accessed objects are not prematurely removed from the cache if there are no references to them.
    • 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 HashMap 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​(DBRecord objectRecord)
      Retrieves the database object with the given record and associated key from the cache.
      java.util.List<T> getCachedObjects()
      Returns an List of all the cached objects.
      void invalidate()
      Marks all the cached objects as invalid.
      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​(DBRecord 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(DBRecord) method which by default ignores the record and simply calls DatabaseObject.refresh().
        Parameters:
        objectRecord - 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.
        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.List<T> getCachedObjects()
        Returns an List of all the cached objects.
        Returns:
        an List of all the cached objects.
      • delete

        public void delete​(java.util.List<KeyRange> keyRanges)
        Delete all objects from HashMap 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.
      • 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.
      • keyChanged

        public void keyChanged​(long oldKey,
                               long newKey)