disklru
raw JSON → 2.0.4 verified Fri May 01 auth: no python
A disk-based LRU (Least Recently Used) cache for Python that stores cached data on disk, providing persistent caching across sessions. Current version 2.0.4 supports Python >=3.8, with a simple decorator and function-based API. Release cadence is irregular, with updates as needed.
pip install disklru Common errors
error ImportError: cannot import name 'discache' from 'disklru' ↓
cause The `discache` function was removed in version 2.0.0.
fix
Use
from disklru import DiskLRU and instantiate it: cache = DiskLRU(...) then @cache.cache() error AttributeError: 'DiskLRU' object has no attribute 'cache' ↓
cause Using `@cache.cache` without calling `DiskLRU` first, or misspelling the method name.
fix
Ensure you have created a DiskLRU instance, and use
@cache_instance.cache() decorator (with parentheses). Warnings
breaking In version 2.0.0, the API changed significantly. The old `discache` function is removed. Use `DiskLRU` class with `@cache.cache()` decorator instead of `@discache()`. Also, the `lru_cache` decorator was added. ↓
fix Replace `from disklru import discache` with `from disklru import DiskLRU` and use `cache = DiskLRU(...); @cache.cache()`
gotcha The cache directory persists between runs. If you change the function logic, you must manually clear the cache directory to invalidate stale entries. ↓
fix Delete the cache_dir or implement versioning in the cache key.
gotcha Pickle is used for serialization, so cached objects must be picklable. Custom objects may fail. ↓
fix Ensure all cached data can be serialized with pickle.
Imports
- DiskLRU wrong
from disklru import discachecorrectfrom disklru import DiskLRU - lru_cache
from disklru import lru_cache
Quickstart
from disklru import DiskLRU
cache = DiskLRU(capacity=10, cache_dir='/tmp/my_cache')
@cache.cache()
def expensive_function(key):
return f'result for {key}'
print(expensive_function('a')) # cached