repoze.lru: Tiny LRU Cache

0.7 · active · verified Fri Apr 10

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.

Warnings

Install

Imports

Quickstart

Demonstrates how to create and use an `LRUCache` object, and how to apply the `lru_cache` decorator to a function.

from repoze.lru import LRUCache, lru_cache

# Using LRUCache object
cache = LRUCache(100)  # Max length of 100 items

cache.put('key1', 'value1')
print(f"Cache get 'key1': {cache.get('key1')}")
print(f"Cache get 'nonexisting' with default: {cache.get('nonexisting', 'default_value')}")

# Using lru_cache decorator
@lru_cache(50)
def expensive_function(arg1, arg2):
    print(f"Executing expensive_function({arg1}, {arg2})")
    return arg1 + arg2

print(f"Result 1: {expensive_function(1, 2)}") # Executes
print(f"Result 2: {expensive_function(1, 2)}") # Cached
print(f"Result 3: {expensive_function(3, 4)}") # Executes

view raw JSON →