Least recently used object cache¶
-
class
larch.lru.
LRUCache
(max_size, remove_hook=None, forget_hook=None)¶ A least-recently-used cache.
This class caches objects, based on keys. The cache has a fixed size, in number of objects. When a new object is added to the cache, the least recently used old object is dropped. Each object is associated with a key, and use is defined as retrieval of the object using the key.
Two hooks are provided for: for removing an object by user request, and when it is automatically removed due to cache overflow. Either hook is called with the key and object as arguments.
-
add
(key, obj)¶ Add new item to cache.
-
get
(key)¶ Retrieve item from cache.
Return object associated with key, or None.
-
keys
()¶ List keys for objects in cache.
-
log_stats
()¶
-
remove
(key)¶ Remove an item from the cache.
Return True if item was in cache, False otherwise.
-
remove_oldest
()¶ Remove oldest object.
Return key and object.
-