Class Cache

java.lang.Object
com.mckoi.util.Cache

public class Cache extends Object
Represents a cache of Objects. A Cache is similar to a Hashtable, in that you can 'add' and 'get' objects from the container given some key. However a cache may remove objects from the container when it becomes too full.

The cache scheme uses a doubly linked-list hashtable. The most recently accessed objects are moved to the start of the list. The end elements in the list are wiped if the cache becomes too full.

Author:
Tobias Downer
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Cache(int max_size)
     
    Cache(int max_size, int clean_percentage)
     
    Cache(int hash_size, int max_size, int clean_percentage)
    The Constructors.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    This is called whenever at Object is put into the cache.
    protected final int
    Cleans away some old elements in the cache.
    void
     
    final Object
    get(Object key)
    If the cache contains the cell with the given key, this method will return the object.
    protected final int
    Deprecated. 
    final int
    Returns the number of nodes that are currently being stored in the cache.
    protected void
    notifyGetWalks(long total_walks, long total_get_ops)
    Notifies that some statistical information about the hash map has updated.
    protected void
    Notifies that the given object has been wiped from the cache by the clean up procedure.
    final void
    put(Object key, Object ob)
    Puts an Object into the cache with the given key.
    final Object
    Ensures that there is no cell with the given key in the cache.
    void
    Clear the cache of all the entries.
    protected boolean
    Returns true if the clean-up method that periodically cleans up the cache, should clean up more elements from the cache.

    Methods inherited from class java.lang.Object

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

    • Cache

      public Cache(int hash_size, int max_size, int clean_percentage)
      The Constructors. It takes a maximum size the cache can grow to, and the percentage of the cache that is wiped when it becomes too full.
    • Cache

      public Cache(int max_size, int clean_percentage)
    • Cache

      public Cache(int max_size)
    • Cache

      public Cache()
  • Method Details

    • getHashSize

      protected final int getHashSize()
      Deprecated.
      Creates the HashMap object to store objects in this cache. This is available to be overwritten.
    • checkClean

      protected void checkClean()
      This is called whenever at Object is put into the cache. This method should determine if the cache should be cleaned and call the clean method if appropriate.
    • shouldWipeMoreNodes

      protected boolean shouldWipeMoreNodes()
      Returns true if the clean-up method that periodically cleans up the cache, should clean up more elements from the cache.
    • notifyWipingNode

      protected void notifyWipingNode(Object ob)
      Notifies that the given object has been wiped from the cache by the clean up procedure.
    • notifyGetWalks

      protected void notifyGetWalks(long total_walks, long total_get_ops)
      Notifies that some statistical information about the hash map has updated. This should be used to compile statistical information about the number of walks a 'get' operation takes to retreive an entry from the hash.

      This method is called every 8192 gets.

    • nodeCount

      public final int nodeCount()
      Returns the number of nodes that are currently being stored in the cache.
    • put

      public final void put(Object key, Object ob)
      Puts an Object into the cache with the given key.
    • get

      public final Object get(Object key)
      If the cache contains the cell with the given key, this method will return the object. If the cell is not in the cache, it returns null.
    • remove

      public final Object remove(Object key)
      Ensures that there is no cell with the given key in the cache. This is useful for ensuring the cache does not contain out-dated information.
    • removeAll

      public void removeAll()
      Clear the cache of all the entries.
    • clear

      public void clear()
    • clean

      protected final int clean()
      Cleans away some old elements in the cache. This method walks from the end, back 'wipe_count' elements putting each object on the recycle stack.