moka-py
raw JSON → 0.3.0 verified Fri May 01 auth: no python
A high performance caching library for Python, written in Rust with PyO3. Provides a concurrent hashmap cache with TTL, TTI, size-based eviction, and a decorator for async/sync functions. Current version 0.3.0, actively maintained, monthly releases.
pip install moka-py Common errors
error ImportError: cannot import name 'Cache' from 'moka' ↓
cause moka-py may not be installed, or you are using an older version without the `Cache` export.
fix
Run
pip install --upgrade moka-py and verify version >=0.1.0. error TypeError: 'NoneType' object is not subscriptable ↓
cause Cache.get() returned None because the key does not exist or expired.
fix
Check the return value for None before subscripting, or use
cache.get'with a default: cache.get(key, 'default'). Warnings
gotcha The Cache is concurrent and thread-safe, but `get` returns None if the key is missing or expired. Do not assume a key exists after insertion without checking. ↓
fix Use cache.get('key') and handle None, or use cache.get_or_insert('key', default_value).
gotcha The `@cached` decorator for async functions requires `wait_concurrent=True` in versions <0.2.1; from 0.2.1+ it is the default for async functions. ↓
fix Update to >=0.2.1 or pass `wait_concurrent=True` explicitly.
deprecated The `remove` method's `default` parameter (introduced in 0.1.15) may be removed in future versions. Use `pop` instead. ↓
fix Use `cache.pop(key, default=None)` instead of `cache.remove(key, default=None)`.
Imports
- Cache
from moka import Cache
Quickstart
from moka import Cache
cache = Cache(max_capacity=100, time_to_live_seconds=300)
cache.insert('key', 'value')
print(cache.get('key'))