aio-libs / aio-libs/aiocache

Lifecycle management of decorators

Open
#609 7 comments 4 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.4k
Forks
181
PR merge metrics
No merged PRs in 30d

Description

Cache objects should be closed at the end of an application lifecycle, with `await Cache.close()`, or using `async with`.

The current design of decorators creates a new cache instance with each decorator and no attempt to close it is made, thus failing to manage this lifecycle management properly.

e.g.
```
@cached(...) # New cache is created here, but never closed.
def foo(): ...
```

We also want to consider using aiojobs for managing some background tasks, but this additionally requires being created within a running loop, something which is unlikely when a decorator is called.

----

One solution I can think of, is to explicitly manage the caches, and pass them to the decorators. This may also need a `.start()` method to initiate the cache later. e.g.

```
cache = Cache(...)

@cached(cache)
def foo(): ...

async def main():
# Initialise application
cache.start()
# Run application
...
# Cleanup application
await cache.close()
```

Or more succinctly:
```
async def main():
async with cache:
# Run application
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.