### Install async-lru Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Install the async-lru package using pip. ```shell pip install async-lru ``` -------------------------------- ### Run Benchmarks Locally Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Instructions to install development dependencies and run local benchmarks using pytest with CodSpeed. ```shell pip install -r requirements-dev.txt pytest --codspeed benchmark.py ``` -------------------------------- ### Basic Usage of async-lru Cache Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Demonstrates how to use the @alru_cache decorator for an async function. Ensures concurrent calls to the wrapped function result in a single execution, with all awaiters receiving the result upon completion. Includes cache info retrieval and closing the cache. ```python import asyncio import aiohttp from async_lru import alru_cache @alru_cache(maxsize=32) async def get_pep(num): resource = 'http://www.python.org/dev/peps/pep-%04d/' % num async with aiohttp.ClientSession() as session: try: async with session.get(resource) as s: return await s.read() except aiohttp.ClientError: return 'Not Found' async def main(): for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991: pep = await get_pep(n) print(n, len(pep)) print(get_pep.cache_info()) # CacheInfo(hits=3, misses=8, maxsize=32, currsize=8) # closing is optional, but highly recommended await get_pep.cache_close() asyncio.run(main()) ``` -------------------------------- ### Async-LRU Cache with TTL Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Applies a time-to-live (TTL) of 5 seconds to the cache for the decorated async function. Cache entries will expire after this duration. ```python @alru_cache(ttl=5) async def func(arg): return arg * 2 ``` -------------------------------- ### Handling Multiple Event Loops with Threading Local Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Provides a pattern for managing separate cache instances per event loop when using multiple loops, typically in multi-threaded asyncio applications. Uses `threading.local` to store cache instances. ```python import threading _local = threading.local() def get_cached_fetcher(): if not hasattr(_local, 'fetcher'): @alru_cache(maxsize=100) async def fetch_data(key): ... _local.fetcher = fetch_data return _local.fetcher ``` -------------------------------- ### Reusing Logic Across Event Loops Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Shows how to reuse the logic of an already decorated function in a new event loop by accessing its `__wrapped__` attribute and reapplying the decorator. ```python @alru_cache(maxsize=32) async def my_task(x): ... # In Loop 1: # my_task() uses the default global cache instance # In Loop 2 (or a new thread): # Create a fresh cache instance for the same logic cached_task_loop2 = alru_cache(maxsize=32)(my_task.__wrapped__) await cached_task_loop2(x) ``` -------------------------------- ### Explicit Cache Invalidation Source: https://github.com/bobthebuidler/faster-async-lru/blob/master/README.rst Demonstrates how to explicitly invalidate a specific cache entry for a function using `cache_invalidate()`. Returns True if the entry was cached, False otherwise. ```python @alru_cache(ttl=5) async def func(arg1, arg2): return arg1 + arg2 func.cache_invalidate(1, arg2=2) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.