{"id":2748,"library":"repoze.lru","title":"repoze.lru: Tiny LRU Cache","description":"repoze.lru is a light-weight LRU (Least Recently Used) cache implementation for Python, including both an `LRUCache` object and an `lru_cache` decorator. It efficiently evicts less frequently used keys and values to manage memory. The current version 0.7, released in 2017, maintains a stable and slow release cadence, focusing on core functionality.","status":"active","version":"0.7","language":"en","source_language":"en","source_url":"https://github.com/repoze/repoze.lru","tags":["cache","lru","decorator","performance","utility"],"install":[{"cmd":"pip install repoze.lru","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"LRUCache","correct":"from repoze.lru import LRUCache"},{"note":"repoze.lru.lru_cache is a separate implementation. It does not support keyword arguments and has different behavior compared to the built-in functools.lru_cache (Python 3.2+).","wrong":"from functools import lru_cache","symbol":"lru_cache","correct":"from repoze.lru import lru_cache"}],"quickstart":{"code":"from repoze.lru import LRUCache, lru_cache\n\n# Using LRUCache object\ncache = LRUCache(100)  # Max length of 100 items\n\ncache.put('key1', 'value1')\nprint(f\"Cache get 'key1': {cache.get('key1')}\")\nprint(f\"Cache get 'nonexisting' with default: {cache.get('nonexisting', 'default_value')}\")\n\n# Using lru_cache decorator\n@lru_cache(50)\ndef expensive_function(arg1, arg2):\n    print(f\"Executing expensive_function({arg1}, {arg2})\")\n    return arg1 + arg2\n\nprint(f\"Result 1: {expensive_function(1, 2)}\") # Executes\nprint(f\"Result 2: {expensive_function(1, 2)}\") # Cached\nprint(f\"Result 3: {expensive_function(3, 4)}\") # Executes","lang":"python","description":"Demonstrates how to create and use an `LRUCache` object, and how to apply the `lru_cache` decorator to a function."},"warnings":[{"fix":"Ensure all arguments to functions decorated with `repoze.lru.lru_cache` are passed positionally. If keyword argument support is needed, consider `functools.lru_cache` (Python 3+) or alternative caching libraries.","message":"The `lru_cache` decorator from `repoze.lru` explicitly does not support keyword arguments for decorated functions. All arguments must be positional.","severity":"gotcha","affected_versions":"0.1 - 0.7"},{"fix":"Upgrade to `repoze.lru` version 0.5 or newer to benefit from fixes for thread safety and race conditions. This is critical for applications using the cache in multi-threaded environments.","message":"Versions prior to 0.5 contained known thread safety issues and potential race conditions in `LRUCache.put()` and cache clearing logic. These could lead to incorrect cache state or performance degradation under concurrent access.","severity":"breaking","affected_versions":"< 0.5"},{"fix":"To cache instance methods, consider applying `lru_cache` to a wrapper function inside the `__init__` method, using a weak reference, or refactoring the logic into a static/module-level function that takes necessary instance data as arguments.","message":"Using `lru_cache` (or any `lru_cache` implementation) directly on instance methods of a class can lead to memory leaks. The cache will hold a strong reference to `self` (the instance) for each unique set of arguments, preventing instances from being garbage collected even after they are no longer actively used.","severity":"gotcha","affected_versions":"0.1 - 0.7 (general LRU cache design issue)"},{"fix":"Ensure your project's Python environment is 2.7 or 3.4+ for full compatibility and tested behavior with `repoze.lru`.","message":"The `repoze.lru` library officially supports Python 2.7 and Python 3.4+. While earlier Python 3 versions (e.g., 3.2) were mentioned in older documentation, relying on these for current development with version 0.7 might lead to unexpected issues.","severity":"gotcha","affected_versions":"All versions, specifically Python 3.0-3.3"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}