public abstract class Cache<K,V>
extends Object
Modifier and Type | Class and Description |
---|---|
protected class |
Cache.ListEntry
An entry in the age list maintained by the cache
|
protected class |
Cache.NullIterator
Used to return an iterator that does nothing
|
Modifier and Type | Field and Description |
---|---|
protected EmbeddedList |
ageList
A list, kept sorted by descending age, of all the entries.
|
protected HashMap<K,Cache.ListEntry> |
keyMap
Maintains a mapping of key to ListEntry, for fast key lookups
|
private int |
maxEntries
Maximum number of entries the cache may contain
|
private int |
maxTime
Maximum amount of time (in seconds) an entry can stay in the cache
without being used.
|
Constructor and Description |
---|
Cache(int maxEntries,
int maxTime)
Constructor - sets up the parameters of the cache.
|
Modifier and Type | Method and Description |
---|---|
protected void |
cleanup()
Maintains the maxEntries and maxTime constraints imposed on the cache.
|
void |
clear()
Remove all entries from the cache.
|
boolean |
dependenciesValid(K key)
Check the dependencies of a cache entry, if present.
|
Iterator |
getDependencies(K key)
Get the list of dependencies for a cache entry, if present.
|
boolean |
has(K key)
Checks if the cache currently contains an entry for the given key.
|
long |
lastSet(K key)
Gets the time the entry for the given key was created, or zero if the
key isn't present.
|
protected void |
logAction(String action,
K key,
V value)
Derived classes can override this method to print out log messages
when significant things happen (entries are added, removed, expired,
etc.)
|
V |
remove(K key)
Remove an entry from the cache.
|
int |
size()
Tells how many entries are currently cached
|
private int maxEntries
private int maxTime
protected HashMap<K,Cache.ListEntry> keyMap
protected EmbeddedList ageList
public Cache(int maxEntries, int maxTime)
maxEntries
- Number of entries allowed. If additional entries
are created, older ones will be removed. Zero
means no limit.maxTime
- Time (in seconds) that a cache entry will remain.
If the entry hasn't been used in that time, it
is removed from the cache. Zero means no time
limit.public boolean has(K key)
key
- The key to look for.public long lastSet(K key)
key
- The key to look forpublic boolean dependenciesValid(K key)
key
- The key to checkpublic Iterator getDependencies(K key)
key
- The key to checkpublic V remove(K key)
key
- The key to look uppublic void clear()
public int size()
protected void cleanup()
protected void logAction(String action, K key, V value)
action
- What happened ("Added", "Removed", etc.)key
- The key involved in the actionvalue
- The value involved in the action