| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private int | clock
Increases with every operation (until renormalised by |
| private final int | CLOCK_THRESHOLD
Threshold at which we re-normalise times using |
| private final int | limit
Maximum size of cache as requested. |
| private final HashMap | |
| private final int | upperLimit
Working upper limit (> limit) at which to evict cached items. |
| Access | Constructor and Description |
|---|---|
| public | LimitedCache(int
the number of entries required capacity)Construct a cache that will hold (at least) the specified number of entries. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | add(K
against which to cache the value key, V to store value)Add a value corresponding to a given key. |
| private void | evictLowest()
Reduce the working size of the cache to at most the originally-specified capacity by removing low-scoring entries. |
| public synchronized V | Returns: the cached value ornull if not presentagainst which cached key)Get a value corresponding to the key, if it was previously cached. |
| private void | |
| public String |
| clock | back to summary |
|---|---|
| private int clock Increases with every operation (until renormalised by | |
| CLOCK_THRESHOLD | back to summary |
|---|---|
| private final int CLOCK_THRESHOLD Threshold at which we re-normalise times using | |
| limit | back to summary |
|---|---|
| private final int limit Maximum size of cache as requested. | |
| map | back to summary |
|---|---|
| private final HashMap<K, LimitedCache. | |
| upperLimit | back to summary |
|---|---|
| private final int upperLimit Working upper limit (> limit) at which to evict cached items. | |
| LimitedCache | back to summary |
|---|---|
| public LimitedCache(int capacity) Construct a cache that will hold (at least) the specified number of entries. (It will sometimes contain a few more so we don't have to scan the cache with every addition.)
| |
| add | back to summary |
|---|---|
| public synchronized void add(K key, V value) Add a value corresponding to a given key.
| |
| evictLowest | back to summary |
|---|---|
| private void evictLowest() Reduce the working size of the cache to at most the originally-specified capacity by removing
low-scoring entries. We only do this when adding to the cache, and only then if the
| |
| get | back to summary |
|---|---|
| public synchronized V get(K key) Get a value corresponding to the key, if it was previously cached.
| |
| scaleClock | back to summary |
|---|---|
| private void scaleClock() Scale down all the last used times and the current clock. We do this because otherwise the
clock increases indefinitely and theoretically could overflow, at which point the algorithm
breaks. We don't do this very often: only when | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | |
| public String |
| Holder | back to summary |
|---|---|
| pack-priv Holder(T value, int time) | |
| score | back to summary |
|---|---|
| pack-priv int score() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |