py-memoize
raw JSON → 3.1.1 verified Mon Apr 27 auth: no python
A caching library for asynchronous Python applications (asyncio-based) that handles dogpiling properly and provides a configurable & extensible API. Current version 3.1.1. Release cadence is irregular.
pip install py-memoize Common errors
error AttributeError: module 'memoize' has no attribute 'Memoizer' ↓
cause Incorrect import path; maybe using 'from memoize import memoize' or similar.
fix
Use: from memoize import Memoizer
error TypeError: 'Memoizer' object is not callable ↓
cause Trying to use the decorator without calling it (missing parentheses).
fix
Use @memoizer.cache(ttl=...) instead of @memoizer.cache.
error RuntimeError: Cannot use sync function with async decorator ↓
cause Decorating a synchronous function with the cache decorator.
fix
Make the function async: async def my_func(...) ...
error ImportError: cannot import name 'TornadoBackend' from 'memoize' ↓
cause Tornado support was removed in v3.0.0.
fix
Use asyncio backends like InMemoryCache or ExpiringCache.
Warnings
breaking Version 3.0.0 removed support for Tornado. If you rely on Tornado integration, stay on v2.x. ↓
fix Use asyncio-based applications or pin version to <3.0.0.
breaking Version 2.0.0 changed exception handling: exceptions are now chained (via __cause__) instead of being added to args. Timeout errors are now also chained. ↓
fix Update any exception handling logic that relied on checking exception.args for chained exceptions.
gotcha The default cache is InMemoryCache which has no expiration by default. Use ExpiringCache if you need TTL-based expiry. ↓
fix Explicitly instantiate Memoizer with an ExpiringCache instance and configure ttl in the decorator.
gotcha The decorator requires the wrapped function to be async. Applying it to a sync function will cause a runtime error. ↓
fix Ensure the decorated function is defined with async def.
deprecated Tornado support was deprecated in v1.2.0 and removed in v3.0.0. Tornado-specific imports will raise ImportError. ↓
fix Migrate to asyncio-based usage.
Imports
- Memoizer
from memoize import Memoizer - ExpiringCache
from memoize.cache import ExpiringCache - InMemoryCache
from memoize.cache import InMemoryCache
Quickstart
import asyncio
from memoize import Memoizer
from memoize.cache import InMemoryCache, ExpiringCache
memoizer = Memoizer(cache=InMemoryCache())
@memoizer.cache(ttl=5)
async def fetch_data(key: str) -> str:
await asyncio.sleep(1)
return f"data-{key}"
async def main():
result = await fetch_data("my_key")
print(result)
asyncio.run(main())