{"id":2582,"library":"lru-dict","title":"LRU Dict","description":"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.","status":"active","version":"1.4.1","language":"en","source_language":"en","source_url":"https://github.com/amitdev/lru-dict","tags":["lru","cache","dict","performance","cpython","data-structure"],"install":[{"cmd":"pip install lru-dict","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required Python interpreter version for the library.","package":"python","version":">=3.9"}],"imports":[{"symbol":"LRU","correct":"from lru import LRU"}],"quickstart":{"code":"from lru import LRU\n\nl = LRU(5) # Create an LRU container with a capacity of 5\n\nfor i in range(5):\n    l[i] = str(i)\nprint(f\"Initial LRU state: {l.items()}\")\n# Expected: [(4, '4'), (3, '3'), (2, '2'), (1, '1'), (0, '0')]\n\nl[5] = '5' # Add a new item, '0' (LRU) should be evicted\nprint(f\"After adding 5: {l.items()}\")\n# Expected: [(5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')]\n\nl[3] # Accessing an item makes it Most Recently Used (MRU)\nprint(f\"After accessing 3: {l.items()}\")\n# Expected: [(3, '3'), (5, '5'), (4, '4'), (2, '2'), (1, '1')]\n\ndel l[4] # Delete an item\nprint(f\"After deleting 4: {l.items()}\")\n# Expected: [(3, '3'), (5, '5'), (2, '2'), (1, '1')]\n\nl.set_size(3) # Resize the LRU cache\nprint(f\"After resizing to 3: {l.items()}\")\n# Expected: [(3, '3'), (5, '2'), (2, '1')] (order depends on eviction policy details for resize)\n\nprint(f\"Current size: {l.get_size()}\") # Get current capacity\nprint(f\"Has key 5: {5 in l}\")\n\nl.update({1: 'one_updated'}) # Update an existing item\nprint(f\"After updating 1: {l.items()}\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade to `lru-dict` version 1.4.0 or newer: `pip install --upgrade lru-dict`.","message":"Older versions of `lru-dict` (e.g., 1.1.8 and earlier) did not support newer Python versions like 3.11+. Upgrading to `lru-dict >=1.4.0` is required for Python 3.11+ compatibility.","severity":"breaking","affected_versions":"<1.4.0"},{"fix":"Implement external locking mechanisms (e.g., `threading.Lock`) around `lru-dict` operations when used in concurrent contexts.","message":"`lru-dict` is a C-implemented cache and is explicitly noted as not being thread-safe in older documentation. While not always explicitly stated in newer versions, C extensions often require explicit synchronization for thread-safe operations if not designed for it. Use with caution in multi-threaded environments or implement external locking.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Understand the distinction: `lru-dict` manages a collection of items, `functools.lru_cache` memoizes function calls. Choose the appropriate tool for your caching need.","message":"`lru-dict` provides a dictionary-like container, distinct from `functools.lru_cache`, which is a function decorator for memoization. Users expecting automatic function result caching should use `functools.lru_cache` instead, or wrap `lru-dict` in a custom caching decorator.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Like standard Python dictionaries, `lru-dict` keys must be hashable. Using mutable objects (e.g., lists, dictionaries) as keys will result in a `TypeError`. Modifying a mutable key after insertion can lead to incorrect cache behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}