edu.stanford.nlp.util
Class CacheMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
edu.stanford.nlp.util.CacheMap<K,V>
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>
public class CacheMap<K,V>
- extends java.util.LinkedHashMap<K,V>
- implements java.util.Map<K,V>, java.lang.Cloneable, java.io.Serializable
A fixed size hash map with LRU replacement. Can optionally automatically
dump itself out to a file as the cache grows.
- Author:
- Ari Steinberg (ari.steinberg@stanford.edu)
- See Also:
- Serialized Form
| Nested classes/interfaces inherited from class java.util.AbstractMap |
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V> |
| Nested classes/interfaces inherited from interface java.util.Map |
java.util.Map.Entry<K,V> |
|
Constructor Summary |
CacheMap(int numEntries)
|
CacheMap(int numEntries,
float loadFactor)
|
CacheMap(int numEntries,
float loadFactor,
boolean accessOrder)
|
CacheMap(int numEntries,
float loadFactor,
boolean accessOrder,
java.lang.String backingFile)
Constructor. |
|
Method Summary |
static
|
create(int numEntries,
float loadFactor,
boolean accessOrder,
java.lang.String file,
boolean useFileParams)
Creates a new file-backed CacheMap or loads it in from the specified file
if it already exists. |
static
|
create(int numEntries,
float loadFactor,
java.lang.String file,
boolean useFileParams)
|
static
|
create(int numEntries,
java.lang.String file,
boolean useFileParams)
|
static
|
create(java.lang.String file,
boolean useFileParams)
|
V |
get(java.lang.Object key)
|
void |
printStats(java.io.PrintStream out)
Print out cache stats to the specified stream. |
V |
put(K key,
V value)
Add the entry to the map, and dump the map to a file if it's been a while
since we last did. |
protected boolean |
removeEldestEntry(java.util.Map.Entry<K,V> eldest)
|
void |
write()
Dump out the contents of the cache to the backing file. |
| Methods inherited from class java.util.LinkedHashMap |
clear, containsValue |
| Methods inherited from class java.util.HashMap |
clone, containsKey, entrySet, isEmpty, keySet, putAll, remove, size, values |
| Methods inherited from class java.util.AbstractMap |
equals, hashCode, toString |
| Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Map |
clear, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values |
backingFile
public java.lang.String backingFile
CacheMap
public CacheMap(int numEntries,
float loadFactor,
boolean accessOrder,
java.lang.String backingFile)
- Constructor.
- Parameters:
numEntries - is the number of entries you want to store in the
CacheMap. This is not the same as the number of
buckets - that is effected by this and the target
loadFactor.accessOrder - is the same as in LinkedHashMap.backingFile - is the name of the file to dump this to, if desired.- See Also:
LinkedHashMap
CacheMap
public CacheMap(int numEntries,
float loadFactor,
boolean accessOrder)
CacheMap
public CacheMap(int numEntries,
float loadFactor)
CacheMap
public CacheMap(int numEntries)
create
public static <K,V> CacheMap<K,V> create(int numEntries,
float loadFactor,
boolean accessOrder,
java.lang.String file,
boolean useFileParams)
- Creates a new file-backed CacheMap or loads it in from the specified file
if it already exists. The parameters passed in are the same as the
constructor. If useFileParams is true and the file exists, all of your
parameters will be ignored (replaced with those stored in the file
itself). If useFileParams is false then we override the settings in the
file with the ones you specify (except loadFactor and accessOrder) and
reset the stats.
create
public static <K,V> CacheMap<K,V> create(int numEntries,
float loadFactor,
java.lang.String file,
boolean useFileParams)
create
public static <K,V> CacheMap<K,V> create(int numEntries,
java.lang.String file,
boolean useFileParams)
create
public static <K,V> CacheMap<K,V> create(java.lang.String file,
boolean useFileParams)
write
public void write()
- Dump out the contents of the cache to the backing file.
removeEldestEntry
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
- Overrides:
removeEldestEntry in class java.util.LinkedHashMap<K,V>
- See Also:
LinkedHashMap.removeEldestEntry(java.util.Map.Entry)
get
public V get(java.lang.Object key)
- Specified by:
get in interface java.util.Map<K,V>- Overrides:
get in class java.util.LinkedHashMap<K,V>
- See Also:
HashMap.get(java.lang.Object)
put
public V put(K key,
V value)
- Add the entry to the map, and dump the map to a file if it's been a while
since we last did.
- Specified by:
put in interface java.util.Map<K,V>- Overrides:
put in class java.util.HashMap<K,V>
- See Also:
HashMap.put(K, V)
printStats
public void printStats(java.io.PrintStream out)
- Print out cache stats to the specified stream. Note that in many cases
treating puts as misses gives a better version of hit percentage than
actually using misses, since it's possible that some of your misses are
because you wind up choosing not to cache the particular value (we output
both versions). Stats are reset when the cache is loaded in from disk
but are otherwise cumulative.
Stanford NLP Group