cached-property (current version 2.0.1) is a Python decorator for caching the results of properties in classes. It provides a straightforward way to memoize expensive computations, executing them only once per instance and storing the result as a regular attribute. The library offers basic, thread-safe, and time-based (TTL) caching mechanisms, as well as experimental async/await compatibility. It maintains an active development status with releases typically aligned with Python version support updates.
pip install cached-propertyVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `cached_property` decorator to cache an expensive property computation. The `boardwalk` property will only execute its logic once per instance, returning the cached value on subsequent accesses until explicitly invalidated.
Upgrade your Python environment to 3.8+ or pin `cached-property` to a compatible version (e.g., `cached-property<2.0.0` for Python <3.8).
For basic caching in Python 3.8+, consider using `from functools import cached_property`. If you require TTL or explicit thread-safe caching, `cached-property` remains a viable choice.
Be aware of this limitation when using TTL features. If immediate invalidation is critical, consider using the base `cached_property` and managing invalidation manually (`del obj.property_name`) or designing around the TTL expiration.
Avoid mixing `asyncio` with threading if possible. If necessary, ensure proper synchronization and careful management of event loops across threads to prevent data corruption or incorrect cached values.
Prefer `del obj.property_name` for simple cache invalidation. Ensure your application logic correctly triggers invalidation when the underlying data changes, as the decorator itself does not automatically detect state changes.
To use the `cached-property` library (version 2.0.1 requires Python 3.8+), ensure it's installed (`pip install cached-property`) and import it directly: `from cached_property import cached_property`. If you specifically intend to use the standard library `functools.cached_property`, ensure your Python environment is version 3.8 or newer.
If you need a writable property with caching, you typically manage cache invalidation manually within a custom setter (e.g., `del self.__dict__['my_property']`) or by defining a standard `@property` with a `@my_property.setter` and implementing explicit caching logic there.
To use `cached_property` with `__slots__`, you must include `'__dict__'` in your `__slots__` definition (e.g., `__slots__ = ('my_attribute', '__dict__')`) to provide a mutable dictionary for caching. Alternatively, avoid `__slots__` if memory optimization is not critical, or consider using `functools.lru_cache` on a regular method if applicable.For thread-safe caching with the `cached-property` library, use the `threaded_cached_property` decorator (`from cached_property import threaded_cached_property`). If you were relying on the removed `functools.cached_property` lock, you must implement explicit locking within your getter function or around access points to ensure thread synchronization.
No dependency data recorded yet.