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 Object
implements Retriever<K,V>

An efficient Map-like class which caches values for keys. Values are not "put" into a Cache; 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.


Nested Class Summary
static interface Cache.MatchPredicate<T>
          Used by {#link #removeKeysMatching(Object)} to decide things that are matching.
 
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.
 void removeKeysMatching(Cache.MatchPredicate<K> predicate)
          Clears all cache entries whose key matches the given predicate.
 void removeValueMatching(Cache.MatchPredicate<V> predicate)
          Clears all cache entries whose value matches the given predicate.
 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

removeKeysMatching

public void removeKeysMatching(Cache.MatchPredicate<K> predicate)
Clears all cache entries whose key matches the given predicate.


removeValueMatching

public void removeValueMatching(Cache.MatchPredicate<V> predicate)
Clears all cache entries whose value matches the given predicate.


clear

public void clear()

Clears the cache.


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.