{"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":"python","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,"search_vec":"'0.7':44 '2017':47 'cach':4,15,27,59 'cadenc':54 'core':57 'current':42 'decor':28,61 'effici':30 'evict':31 'focus':55 'frequent':33 'function':58 'implement':16 'includ':19 'key':35 'least':12 'less':32 'light':9 'light-weight':8 'lru':3,11,26,60 'lrucach':22 'maintain':48 'manag':39 'memori':40 'object':23 'perform':62 'python':18 'recent':13 'releas':45,53 'repoze.lru':1,5 'slow':52 'stabl':50 'tini':2 'use':14,34 'util':63 'valu':37 'version':43 'weight':10","created_at":"2026-04-11T01:41:09.368794+00:00","updated_at":"2026-04-11T01:41:09.368794+00:00","problems":[],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.8","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/repoze/repoze.lru","docs":"https://repozelru.readthedocs.io/en/stable/index.html","changelog":"https://github.com/repoze/repoze.lru/blob/master/CHANGES.rst","pypi":"https://pypi.org/project/repoze.lru/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}