org.hypergraphdb.cache
Interface HGCache<Key,Value>

All Known Implementing Classes:
MRUCache, SimpleCache

public interface HGCache<Key,Value>

A simple generic, read-only caching interface. An implementation must be initialized by providing the means to load data that is not in the cache - a RefResolver instance. Therefore, there's no put method, the cache makes the decision if and when to actually keep data in it. Consequently, the cache is operational only when there is RefResolver currently in effect. Otherwise, the get will simply throw a NullPointerException when attempting to access it.

Note, however, that there is a remove to explicitly remove an element from the cache. This method should generally be called only when the item is being removed from permanent storage as well.

When and how element are purged from the cache is not mandated by the interface.

NOTE: Implementation are expected to be thread-safe.

Author:
Borislav Iordanov

Method Summary
 void clear()
          Clear (i.e.
 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 setResolver(RefResolver<Key,Value> resolver)
          Set the RefResolver to be used to load data in the cache.
 

Method Detail

setResolver

void setResolver(RefResolver<Key,Value> resolver)

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


getResolver

RefResolver<Key,Value> getResolver()

Return the RefResolver used to load data in the cache.


get

Value get(Key key)

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.

Parameters:
key - The key of the element.
Returns:
The element's value.

getIfLoaded

Value getIfLoaded(Key key)

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.

Parameters:
key - The key of the element.
Returns:
The element's value or null is it's not found in the cache.

isLoaded

boolean isLoaded(Key key)

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


remove

void remove(Key key)

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.

Parameters:
key - The key of the element.

clear

void clear()

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