class documentation

A class which manages a cache of entries, removing unused ones.

Method __contains__ Check if key is in cache.
Method __getitem__ Get item from cache and mark as recently used.
Method __init__ Initialize LRUCache.
Method __len__ Return number of items in cache.
Method __setitem__ Add a value to the cache, there will be no cleanup function.
Method add Add a new value to the cache.
Method cache_size Get the number of entries we will cache.
Method cleanup Clear the cache until it shrinks to the requested size.
Method clear Clear out all of the cache.
Method get Get value from cache with default if not found.
Method items Get the key:value pairs as a dict.
Method keys Get the list of keys currently cached.
Method resize Change the number of entries that will be cached.
Method _record_access Record that key was accessed.
Method _remove_lru Remove one entry from the lru, and handle consequences.
Method _remove_node Undocumented
Method _update_max_cache Undocumented
Method _walk_lru Walk the LRU list, only meant to be used in tests.
Instance Variable _after_cleanup_count Undocumented
Instance Variable _cache Undocumented
Instance Variable _least_recently_used Undocumented
Instance Variable _max_cache Undocumented
Instance Variable _most_recently_used Undocumented
def __contains__(self, key: K) -> bool:

Check if key is in cache.

def __getitem__(self, key: K) -> V:

Get item from cache and mark as recently used.

def __init__(self, max_cache: int = 100, after_cleanup_count: int | None = None):

Initialize LRUCache.

Parameters
max_cache:intMaximum number of entries to cache
after_cleanup_count:int | NoneNumber of entries to keep after cleanup
def __len__(self) -> int:

Return number of items in cache.

def __setitem__(self, key: K, value: V):

Add a value to the cache, there will be no cleanup function.

def add(self, key: K, value: V, cleanup: Callable[[K, V], None] | None = None):

Add a new value to the cache.

Also, if the entry is ever removed from the cache, call cleanup(key, value).

Parameters
key:KThe key to store it under
value:VThe object to store
cleanup:Callable[[K, V], None] | NoneNone or a function taking (key, value) to indicate 'value' should be cleaned up.
def cache_size(self) -> int:

Get the number of entries we will cache.

def cleanup(self):

Clear the cache until it shrinks to the requested size.

This does not completely wipe the cache, just makes sure it is under the after_cleanup_count.

def clear(self):

Clear out all of the cache.

def get(self, key: K, default: V | None = None) -> V | None:

Get value from cache with default if not found.

Parameters
key:KKey to look up
default:V | NoneDefault value if key not found
Returns
V | NoneValue from cache or default
def items(self) -> dict[K, V]:

Get the key:value pairs as a dict.

def keys(self) -> Iterable[K]:

Get the list of keys currently cached.

Note that values returned here may not be available by the time you request them later. This is simply meant as a peak into the current state.

Returns: An unordered list of keys that are currently cached.

def resize(self, max_cache: int, after_cleanup_count: int | None = None):

Change the number of entries that will be cached.

def _record_access(self, node: _LRUNode[K, V]):

Record that key was accessed.

def _remove_lru(self):

Remove one entry from the lru, and handle consequences.

If there are no more references to the lru, then this entry should be removed from the cache.

def _remove_node(self, node: _LRUNode[K, V]):

Undocumented

def _update_max_cache(self, max_cache: int, after_cleanup_count: int | None = None):

Undocumented

def _walk_lru(self) -> Iterator[_LRUNode[K, V]]:

Walk the LRU list, only meant to be used in tests.

_after_cleanup_count =

Undocumented

_cache: dict[K, _LRUNode[K, V]] =

Undocumented

_least_recently_used: _LRUNode[K, V] | None =

Undocumented

_max_cache =

Undocumented

_most_recently_used: _LRUNode[K, V] | None =

Undocumented