org.hypergraphdb.cache
Class MRUCache<Key,Value>

java.lang.Object
  extended by org.hypergraphdb.cache.MRUCache<Key,Value>
Type Parameters:
Key -
Value -
All Implemented Interfaces:
HGCache<Key,Value>, CloseMe

public class MRUCache<Key,Value>
extends java.lang.Object
implements HGCache<Key,Value>, CloseMe

Implements a cache that keeps most recently used elements in memory while discarding the least recently used ones. Evicting elements is done in chunks determined by a percentage of the current cache's size - see the constructors for more info.

Author:
Borislav Iordanov

Constructor Summary
MRUCache()
           
MRUCache(float usedMemoryThreshold, float evictPercent)
           
MRUCache(int maxSize, int evictCount)
           
MRUCache(java.util.concurrent.locks.ReadWriteLock lockImplementation)
           
 
Method Summary
 void checkConsistent()
          Check that the map contains exactly the same elements as the linked list.
 void clear()
          Clear (i.e.
 void clearNonBlocking()
           
 void close()
           
 Value get(Key key)
          Retrieve an element from the cache.
 Value getIfLoaded(Key key)
          Retrieve and return an element from the cache if it's already there or return null otherwise.
 RefResolver<Key,Value> getResolver()
          Return the RefResolver used to load data in the cache.
 boolean isLoaded(Key key)
          Return true if the element with the given key is currently in the cache and false otherwise.
 void remove(Key key)
          Force removal of an element from the cache.
 void setLockImplementation(java.util.concurrent.locks.ReadWriteLock lockImplementation)
           
 void setResolver(RefResolver<Key,Value> resolver)
          Set the RefResolver to be used to load data in the cache.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MRUCache

public MRUCache()

MRUCache

public MRUCache(java.util.concurrent.locks.ReadWriteLock lockImplementation)

MRUCache

public MRUCache(int maxSize,
                int evictCount)
Parameters:
maxSize - The maximum number of elements allowed in the cache.
evictCount - The number of (least used) elements to evict when the cache reaches its maximum.

MRUCache

public MRUCache(float usedMemoryThreshold,
                float evictPercent)
Parameters:
usedMemoryThreshold - The percentage of total memory that must become used before the cache decides to evict elements (e.g. a value of 0.9 means the cache will evict elements when 90% of memory is currently in use).
evictPercent - The percentage of elements to evict when the usedMemoryThreshold is reached.
Method Detail

get

public Value get(Key key)
Description copied from interface: HGCache

Retrieve an element from the cache. If the element is already in the cache, it is simply returned. Otherwise, the RefResolver will be used to obtain it automatically from permanent storage.

Specified by:
get in interface HGCache<Key,Value>
Parameters:
key - The key of the element.
Returns:
The element's value.

getIfLoaded

public Value getIfLoaded(Key key)
Description copied from interface: HGCache

Retrieve and return an element from the cache if it's already there or return null otherwise. This method will not call the RefResolver when the element is not found in the cache.

Specified by:
getIfLoaded in interface HGCache<Key,Value>
Parameters:
key - The key of the element.
Returns:
The element's value or null is it's not found in the cache.

isLoaded

public boolean isLoaded(Key key)
Description copied from interface: HGCache

Return true if the element with the given key is currently in the cache and false otherwise.

Specified by:
isLoaded in interface HGCache<Key,Value>

remove

public void remove(Key key)
Description copied from interface: HGCache

Force removal of an element from the cache. This method is generally used when the data has been (or is being) removed from the permanent storage as well.

Specified by:
remove in interface HGCache<Key,Value>
Parameters:
key - The key of the element.

getResolver

public RefResolver<Key,Value> getResolver()
Description copied from interface: HGCache

Return the RefResolver used to load data in the cache.

Specified by:
getResolver in interface HGCache<Key,Value>

setResolver

public void setResolver(RefResolver<Key,Value> resolver)
Description copied from interface: HGCache

Set the RefResolver to be used to load data in the cache.

Specified by:
setResolver in interface HGCache<Key,Value>

clear

public void clear()
Description copied from interface: HGCache

Clear (i.e. force removal of) all elements from the cache.

Specified by:
clear in interface HGCache<Key,Value>

clearNonBlocking

public void clearNonBlocking()

checkConsistent

public void checkConsistent()
Check that the map contains exactly the same elements as the linked list. Throw an exception if that is not the case. This method is intended for testing. Could be used in a production runtime for monitoring but it should be kept in mind that it might take quite a long time. The method will block all other activity on the cache.


setLockImplementation

public void setLockImplementation(java.util.concurrent.locks.ReadWriteLock lockImplementation)

close

public void close()
Specified by:
close in interface CloseMe