Top Description Inners Fields Constructors Methods
org.python.core.util

public Class LimitedCache<K, V>

extends Object
Class Inheritance
Imports
java.util.Arrays, .HashMap, .Iterator, .Map.Entry

Not for application use, a cache of recently given results from some costly function. This is only public so we can reach it from the run-time. The user sets a nominal size for the cache. The cache will grow to this size and a little more, but from time to time discard the "little more" on the basis of a score that depends on recency of use.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
LimitedCache.Holder<T>

Object that holds one cached value and its statistics.

Field Summary

Modifier and TypeField and Description
private int
clock

Increases with every operation (until renormalised by scaleClock()).

private final int
CLOCK_THRESHOLD

Threshold at which we re-normalise times using scaleClock().

private final int
limit

Maximum size of cache as requested.

private final HashMap<K, LimitedCache.Holder<? extends V>>
private final int
upperLimit

Working upper limit (> limit) at which to evict cached items.

Constructor Summary

AccessConstructor and Description
public
LimitedCache(int
the number of entries required
capacity
)

Construct a cache that will hold (at least) the specified number of entries.

Method Summary

Modifier and TypeMethod 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 or null if not present
get
(K
against which cached
key
)

Get a value corresponding to the key, if it was previously cached.

private void
scaleClock()

Scale down all the last used times and the current clock.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

clockback to summary
private int clock

Increases with every operation (until renormalised by scaleClock()).

CLOCK_THRESHOLDback to summary
private final int CLOCK_THRESHOLD

Threshold at which we re-normalise times using scaleClock(). Big enough it hardly ever happens. Small enough to prevent overflow (much less than max integer).

limitback to summary
private final int limit

Maximum size of cache as requested.

mapback to summary
private final HashMap<K, LimitedCache.Holder<? extends V>> map
upperLimitback to summary
private final int upperLimit

Working upper limit (> limit) at which to evict cached items.

Constructor Detail

LimitedCacheback 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.)

Parameters
capacity:int

the number of entries required

Method Detail

addback to summary
public synchronized void add(K key, V value)

Add a value corresponding to a given key.

Parameters
key:K

against which to cache the value

value:V

to store

evictLowestback 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 upperLimit has been reached.

getback to summary
public synchronized V get(K key)

Get a value corresponding to the key, if it was previously cached.

Parameters
key:K

against which cached

Returns:V

the cached value or null if not present

scaleClockback 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 clock reached CLOCK_THRESHOLD.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override

org.python.core.util back to summary

private Class LimitedCache.Holder<T>

extends Object
Class Inheritance

Object that holds one cached value and its statistics.

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv final T

Constructor Summary

AccessConstructor and Description
pack-priv
Holder(T value, int time)

Method Summary

Modifier and TypeMethod and Description
pack-priv int
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

usedback to summary
pack-priv int used
valueback to summary
pack-priv final T value

Constructor Detail

Holderback to summary
pack-priv Holder(T value, int time)

Method Detail

scoreback to summary
pack-priv int score()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override