Package nltk :: Package wordnet :: Module cache :: Class _LRUCache
[hide private]
[frames] | no frames]

Class _LRUCache

source code

A cache of values such that least recently used element is flushed when the cache fills.

This lets us retrieve the key given the timestamp, and the timestamp given the key. (Also the value given either one.) That's necessary so that we can reorder the history given a key, and also manipulate the values dict given a timestamp.

I haven't tried changing history to a List. An earlier implementation of history as a List was slower than what's here, but the two implementations aren't directly comparable.

Instance Methods [hide private]
 
__init__(self, capacity)
Initialize a new cache
source code
 
clear(self)
Flush the cache
source code
 
removeOldestEntry(self)
Remove the oldest entry from the cache.
source code
 
setCapacity(self, capacity)
Set the capacity of the cache.
source code
 
get(self, key, loadfn=None)
Get an item from the cache.
source code
Instance Variables [hide private]
dict history
A dict from timestamp -> key
int nextTimestamp
Timestamp to use with the next value that's added.
int oldestTimestamp
Timestamp of the oldest element (the next one to remove), or slightly lower than that.
dict values
A dict from key -> (value, timestamp)
Method Details [hide private]

__init__(self, capacity)
(Constructor)

source code 

Initialize a new cache

Parameters:
  • capacity (int) - Size of the cache (number of Sense -> Synset mappings)

setCapacity(self, capacity)

source code 

Set the capacity of the cache.

Parameters:
  • capacity (int) - new size of the cache

get(self, key, loadfn=None)

source code 

Get an item from the cache.

Parameters:
  • key (unknown) - identifier of a cache entry
  • loadfn (function reference) - a function used to load the cached entry
Returns:
a cached item