Provides various memoizing collections and decorators, including variants of Python's @lru_cache function decorator. Current version: 7.0.5. Release cadence: Regular updates with new features and improvements.
pip install cachetoolsVerified import paths — ran on the pinned version, not inferred.
A simple example demonstrating the use of cachetools to memoize Fibonacci number calculations.
Ensure that the cache function always returns a valid cache object.
Replace 'typed=True' with 'key=typedkey' in the cached decorator.
Use a suitable lock object when accessing the cache from multiple threads.
Use a virtual environment for installing Python packages to avoid permission issues and conflicts with the system package manager.
Investigate the meaning of the numerical output `267914296` in the context of the executed test to determine the actual failure mode. If it represents an unexpected return value, compare it against expected behavior for the specific library/function being tested.
Run `pip install cachetools` in your terminal to install the package.
When using cache decorators like `@cached` or `@ttl_cache`, provide a suitable `lock` object (e.g., `lock=threading.Lock()`) for thread-safe operations. If manually managing a cache instance, wrap access with explicit locking using `threading.Lock`.
Provide a custom `key` function to the cache decorator (e.g., `@cached(cache=LRUCache(maxsize=128), key=cachetools.keys.hashkey)`) to transform unhashable arguments into a hashable representation, such as a tuple of sorted items or a unique identifier.
Import the specific cache decorator or class directly from the `cachetools` module (e.g., `from cachetools import cached, LRUCache, TTLCache`) and use them as `@cached(cache=LRUCache(...))` or `@ttl_cache(...)`.
Ensure that the `missing` factory function provided to the cache returns the expected mutable type (e.g., a list `missing=lambda k: []` if you intend to use list methods) or handle the returned dictionary type appropriately without calling list methods on it.