Metadata-Version: 2.3
Name: acachetools
Version: 0.1.6
Summary: Async cachetools decorators with stampede protection.
Author: RektPunk
Author-email: RektPunk <rektpunk@gmail.com>
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: cachetools>=7.1.4
Requires-Python: >=3.10
Project-URL: repository, https://github.com/RektPunk/acachetools
Description-Content-Type: text/markdown

<div style="text-align: center;">
  <img src="https://capsule-render.vercel.app/api?type=transparent&fontColor=0047AB&text=acachetools&height=120&fontSize=90">
</div>

**acachetools** provides asyncio-compatible versions of `cachetools.cached()` and `cachetools.cachedmethod()`, with support for [`cachetools`](https://github.com/tkem/cachetools) cache implementations such as `TTLCache` and `LRUCache` Concurrent calls with the same cache key share a single in-flight computation, preventing cache stampedes.

## Installation

```bash
pip install acachetools
```

## Usage

### `cached`

```python
from cachetools import TTLCache
from acachetools import cached


@cached(cache=TTLCache(maxsize=1024, ttl=600))
async def some_func(value: int): ...
```

### `cachedmethod`

```python
from cachetools import LRUCache
from acachetools import cachedmethod


class SomeClass:
    def __init__(self):
        self.cache = LRUCache(maxsize=128)

    @cachedmethod(lambda self: self.cache)
    async def some_func(self, value: int): ...
```

### Behavior

- **Cancellation:** Cancelling a caller does not cancel the shared computation.
- **Cache invalidation:** `cache_clear()` clears cached results without cancelling in-flight computations.
- **Context propagation:** Each computation runs with a copy of the initiating caller's context.
- **Cache storage failures:** Values that cannot be stored, such as those exceeding `maxsize`, are returned normally but not cached.

### Limitations

- **Event loop and thread safety:** Each decorated function and its cache must be used from a single event loop and thread. Cross-loop access to an in-flight computation raises `RuntimeError`.
- **`cachedmethod` scope:** All instances of a decorated method share its in-flight state, even when they use separate caches. Therefore, the decorated method must also be used from a single event loop and thread.
