asyncache is a Python library providing helpers to easily integrate `cachetools` caching strategies with asynchronous Python code, specifically designed for `asyncio` applications. It allows developers to decorate `async` functions to transparently cache their results. The current version is 0.3.1, released in November 2022, and it appears to be actively maintained through issues and pull requests, though new releases are infrequent.
pip install asyncacheVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `@cached` decorator from `asyncache` with a `TTLCache` from `cachetools` to cache the results of an asynchronous function. It shows cache hits and misses, and how Time-To-Live (TTL) expiration works.
Thoroughly understand `cachetools` policies. Monitor cache hit/miss rates in production to validate your chosen policy and parameters. Adjust `maxsize` and `ttl` as needed, considering memory constraints and data freshness requirements.
Implement explicit cache invalidation mechanisms (e.g., `cache.clear()`, `del cache[key]`) when source data changes. Consider a 'write-through' or 'write-behind' caching strategy if applicable, or adopt a shorter TTL for highly dynamic data. Use unique, deterministic cache keys.
If encountering issues with type checkers or introspection, consider adding `# type: ignore` comments to suppress false positives. Alternatively, evaluate if caching can be applied at a service layer function rather than directly on inherited class methods, or explore explicit caching logic within the method body.
Install the package using pip: 'pip install asyncache'.
Ensure you have the latest version of 'asyncache' installed: 'pip install --upgrade asyncache'.
Ensure that the function decorated with 'cached' is defined as 'async def'.
Verify the import statement: 'from asyncache import cached' and ensure 'asyncache' is up to date.
Instantiate 'TTLCache' correctly: 'cache = TTLCache(maxsize=1024, ttl=60)'.