cachetools: Extensible Memoizing Collections and Decorators

7.0.5 · active · verified Sat Mar 28

Provides various memoizing collections and decorators, including variants of Python's @lru_cache function decorator. Current version: 7.0.5. Release cadence: Regular updates with new features and improvements.

Warnings

Install

Imports

Quickstart

A simple example demonstrating the use of cachetools to memoize Fibonacci number calculations.

import os
from cachetools import cached, LRUCache

# Initialize a cache with a maximum size of 128
cache = LRUCache(maxsize=128)

@cached(cache)
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

# Compute the 42nd Fibonacci number
print(fib(42))

view raw JSON →