lru-dict is a fixed-size dictionary-like container that evicts the Least Recently Used (LRU) items once its capacity limit is exceeded. It is implemented in C for performance and memory efficiency. The library is currently at version 1.4.1 and maintains an active release cadence, focusing on Python version compatibility and minor feature enhancements.
pip install lru-dictVerified import paths — ran on the pinned version, not inferred.
Initializes an LRU cache, demonstrates item insertion, eviction, access-based reordering, deletion, resizing, and updates. Includes examples of `items()`, `keys()`, `get_size()`, `set_size()`, and `in` operator usage.
Upgrade to `lru-dict` version 1.4.0 or newer: `pip install --upgrade lru-dict`.
Implement external locking mechanisms (e.g., `threading.Lock`) around `lru-dict` operations when used in concurrent contexts.
Understand the distinction: `lru-dict` manages a collection of items, `functools.lru_cache` memoizes function calls. Choose the appropriate tool for your caching need.
Ensure that all keys used with `lru-dict` are immutable (e.g., strings, numbers, tuples). If mutable state is necessary, use a hashable representation of that state as the key.
The correct import statement is `from lru import LRU`.
Install the appropriate Python development package for your operating system (e.g., `sudo apt-get install python3-dev` on Debian/Ubuntu, `sudo yum install python3-devel` on RHEL/CentOS).
Ensure `lru-dict` is correctly installed (`pip install lru-dict`) and that there are no conflicting `lru` packages. Verify the import `from lru import LRU`. If a conflicting package is present, uninstall it first.