Metadata-Version: 2.1
Name: cachefunc
Version: 0.2.6
Summary: Caching functions and coroutines result with decorators
Home-page: https://github.com/ma-petrov/cachefunc
License: MIT
Author: Petrov Mikhail
Author-email: petrov.ma@icloud.com
Requires-Python: >=3.9,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Description-Content-Type: text/markdown

# cachefunc

Caching functions and coroutines result with decorators

# Usage

Basic usage with default dict cache:

```python
import aiohttp
import asyncio
import requests
from cachefunc import cachefunc, cachecoro

@cachefunc()
def your_func(url):
    response = requests.get(url)
    return response.text

@cachecoro()
async def your_async_func(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            html = await response.text()
            return html
```

There are two available caches - dict and redis:

```python
from cachefunc import cachefunc, DictCache, RedisCache

dict_cache = DictCache()
redis_cache = RedisCache()

@cachefunc(dict_cache)
def your_func_cached_in_dict(url):
    response = requests.get(url)
    return response.text 

@cachefunc(redis_cache)
def your_func_cached_in_redis(url):
    response = requests.get(url)
    return response.text
```

For DictCache it is possible to setup distinct timeout for each function:

```python
from datetime import timedelta

@cachefunc(dict_cache, timeout=timedelta(hours=2))
def your_func_cached_for_two_hours(url):
    response = requests.get(url)
    return response.text

@cachefunc(dict_cache, timeout=timedelta(minutes=10))
def your_ohter_func_cached_for_ten_minutes(connection, query):
    cursor = connection.execute(query)
    return cursor.fetchall() 
```

Also custom cache can be created from BaseCache, you should implement
methods _get() and _set():

```python
from cachefunc import BaseCache

class CustomCache(BaseCache):
    def _get(self, key: int) -> Any: ...
    def _set(self, key: int, result: Any, **kwargs) -> None: ...
```

## License

[MIT](https://choosealicense.com/licenses/mit/)
