Theine - High performance in-memory cache

raw JSON →
2.0.0 verified Fri May 01 auth: no python

Theine is a high performance in-memory cache library for Python, supporting TTL, LRU, LFU, and clock-based eviction policies. Current version 2.0.0 (release 2025 Q1) requires Python >=3.9. It offers optimized caching for applications needing low latency and high throughput.

pip install theine
error ImportError: cannot import name 'Theine' from 'theine'
cause Breaking change in v2.0.0: the main class was renamed from `Theine` to `Cache`.
fix
Change import to from theine import Cache and update instantiation to Cache('key-value').
error ValueError: 'lru' is not a valid Cache policy
cause Providing an unsupported or misspelled policy string (e.g., 'LRU' with uppercase).
fix
Use exactly the policy names: 'key-value', 'ttl', 'lru', 'lfu', or 'clock'. Ensure lowercase and correct spelling.
breaking In v2.0.0, the import path changed from `theine.Theine` to `theine.Cache` and the constructor signature changed. Code using the old import will raise ImportError.
fix Use `from theine import Cache` and create cache with `Cache('key-value')` instead of `Theine()`.
deprecated The `TTLCache` and `LRUCache` subclasses are deprecated in v2.0.0. Use the unified `Cache` class with the appropriate eviction policy.
fix Replace `TTLCache()` with `Cache('ttl')` and `LRUCache()` with `Cache('lru')`.
gotcha `Cache` policy argument is case-sensitive and must be one of the supported policies: 'key-value', 'ttl', 'lru', 'lfu', 'clock'. Typos like 'LRU' or 'lru ' (with space) will raise ValueError.
fix Ensure the policy string exactly matches the documented options (lowercase, no spaces).

Create a simple key-value cache and perform get/set operations.

from theine import Cache

cache = Cache('key-value')
cache.set('name', 'Theine')
print(cache.get('name'))  # 'Theine'