Pylru is a pure Python library implementing a Least Recently Used (LRU) cache. It provides a simple dictionary-like interface for `lrucache` and includes classes for wrapping existing dictionary-like objects (`WriteThroughCacheManager`) and functions (`FunctionCacheManager`, `lrudecorator`). The library is efficient, offering constant-time basic operations (lookup, insert, delete). The current version is 1.3.1, with releases happening periodically based on maintenance needs.
pip install pylruVerified import paths — ran on the pinned version, not inferred.
This example demonstrates the basic usage of `pylru.lrucache` as a dictionary-like object, showing insertions, lookups, and the LRU eviction policy. It also includes a basic example of the `pylru.lrudecorator` for memoizing function calls.
Iterate over a copy of the keys/items: `for key in list(cache.keys()): ...`
Ensure function arguments are immutable (numbers, strings, tuples) or convert mutable inputs to immutable forms (e.g., `tuple(list_arg)`) before passing them to a decorated function.
Minimize frequent calls to `len()` on `WriteThroughCacheManager` instances if performance is critical. Consider if the exact count is always necessary or if an approximation suffices.
Use `value = cache.get('key', default_value)` to provide a fallback, or check for key existence before access: `if 'key' in cache: value = cache['key']`.Refactor the function to accept only immutable arguments (e.g., numbers, strings, tuples of immutable types). If mutable data must be used, convert it to an immutable representation (e.g., a tuple of its contents) before passing it to the decorated function.
No dependency data recorded yet.