org.apache.mahout.cf.taste.impl.common
Class Cache<K,V>

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.common.Cache<K,V>
All Implemented Interfaces:
Retriever<K,V>

public final class Cache<K,V>
extends java.lang.Object
implements Retriever<K,V>

An efficient Map-like class which caches values for keys. Values are not "put" into a ; instead the caller supplies the instance with an implementation of Retriever which can load the value for a given key.

The cache does not support null keys.

Thanks to Amila Jayasooriya for helping evaluate performance of the rewrite of this class, as part of a Google Summer of Code 2007 project.


Constructor Summary
Cache(Retriever<? super K,? extends V> retriever)
           Creates a new cache based on the given Retriever.
Cache(Retriever<? super K,? extends V> retriever, int maxEntries)
           Creates a new cache based on the given Retriever and with given maximum size.
 
Method Summary
 void clear()
           Clears the cache.
 V get(K key)
           Returns cached value for a key.
 void remove(K key)
           Uncaches any existing value for a given key.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Cache

public Cache(Retriever<? super K,? extends V> retriever)

Creates a new cache based on the given Retriever.

Parameters:
retriever - object which can retrieve values for keys

Cache

public Cache(Retriever<? super K,? extends V> retriever,
             int maxEntries)

Creates a new cache based on the given Retriever and with given maximum size.

Parameters:
retriever - object which can retrieve values for keys
maxEntries - maximum number of entries the cache will store before evicting some
Method Detail

get

public V get(K key)
      throws TasteException

Returns cached value for a key. If it does not exist, it is loaded using a Retriever.

Specified by:
get in interface Retriever<K,V>
Parameters:
key - cache key
Returns:
value for that key
Throws:
TasteException - if an exception occurs while retrieving a new cached value

remove

public void remove(K key)

Uncaches any existing value for a given key.

Parameters:
key - cache key

clear

public void clear()

Clears the cache.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2008-2010 The Apache Software Foundation. All Rights Reserved.