Package com.mckoi.util
Class Cache
- java.lang.Object
-
- com.mckoi.util.Cache
-
- Direct Known Subclasses:
DataCellCache.DCCache
public class Cache extends java.lang.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
Cache.ListNode
An element in the linked list structure.
-
Field Summary
Fields Modifier and Type Field Description private int
current_cache_size
The current cache size.private long
get_total
private Cache.ListNode
list_end
A pointer to the end of the list.private Cache.ListNode
list_start
A pointer to the start of the list.private int
max_cache_size
The maximum number of DataCell objects that can be stored in the cache at any one time.private Cache.ListNode[]
node_hash
The array of ListNode objects arranged by hashing value.private long
total_gets
Some statistics about the hashing algorithm.private int
wipe_to
The number of nodes that should be left available when the cache becomes too full and a clean up operation occurs.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private void
bringToHead(Cache.ListNode node)
Brings 'node' to the start of the list.protected void
checkClean()
This is called whenever at Object is put into the cache.protected int
clean()
Cleans away some old elements in the cache.void
clear()
private void
clearHash()
Clears the entire hashtable of all entries.private Cache.ListNode
createListNode()
Creates a new ListNode.java.lang.Object
get(java.lang.Object key)
If the cache contains the cell with the given key, this method will return the object.private Cache.ListNode
getFromHash(java.lang.Object key)
Finds the node with the given key in the hash table and returns it.protected int
getHashSize()
Deprecated.int
nodeCount()
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
notifyWipingNode(java.lang.Object ob)
Notifies that the given object has been wiped from the cache by the clean up procedure.void
put(java.lang.Object key, java.lang.Object ob)
Puts an Object into the cache with the given key.private Cache.ListNode
putIntoHash(Cache.ListNode node)
Puts the node with the given key into the hash table.java.lang.Object
remove(java.lang.Object key)
Ensures that there is no cell with the given key in the cache.void
removeAll()
Clear the cache of all the entries.private Cache.ListNode
removeFromHash(java.lang.Object key)
Removes the given node from the hash table.protected boolean
shouldWipeMoreNodes()
Returns true if the clean-up method that periodically cleans up the cache, should clean up more elements from the cache.
-
-
-
Field Detail
-
max_cache_size
private int max_cache_size
The maximum number of DataCell objects that can be stored in the cache at any one time.
-
current_cache_size
private int current_cache_size
The current cache size.
-
wipe_to
private int wipe_to
The number of nodes that should be left available when the cache becomes too full and a clean up operation occurs.
-
node_hash
private final Cache.ListNode[] node_hash
The array of ListNode objects arranged by hashing value.
-
list_start
private Cache.ListNode list_start
A pointer to the start of the list.
-
list_end
private Cache.ListNode list_end
A pointer to the end of the list.
-
total_gets
private long total_gets
Some statistics about the hashing algorithm.
-
get_total
private long get_total
-
-
Constructor Detail
-
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 Detail
-
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(java.lang.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.
-
getFromHash
private Cache.ListNode getFromHash(java.lang.Object key)
Finds the node with the given key in the hash table and returns it. Returns 'null' if the value isn't in the hash table.
-
putIntoHash
private Cache.ListNode putIntoHash(Cache.ListNode node)
Puts the node with the given key into the hash table.
-
removeFromHash
private Cache.ListNode removeFromHash(java.lang.Object key)
Removes the given node from the hash table. Returns 'null' if the entry wasn't found in the hash.
-
clearHash
private void clearHash()
Clears the entire hashtable of all entries.
-
nodeCount
public final int nodeCount()
Returns the number of nodes that are currently being stored in the cache.
-
put
public final void put(java.lang.Object key, java.lang.Object ob)
Puts an Object into the cache with the given key.
-
get
public final java.lang.Object get(java.lang.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 java.lang.Object remove(java.lang.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()
-
createListNode
private final Cache.ListNode createListNode()
Creates a new ListNode. If there is a free ListNode on the 'recycled_nodes' then it obtains one from there, else it creates a new blank one.
-
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.
-
bringToHead
private final void bringToHead(Cache.ListNode node)
Brings 'node' to the start of the list. Only nodes at the end of the list are cleaned.
-
-