Caching functions and coroutines result with decorators
Project description
cachefunc
Caching functions and coroutines result with decorators
Usage
Basic usage with default dict cache:
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:
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:
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():
from cachefunc import BaseCache
class CustomCache(BaseCache):
def _get(self, key: int) -> Any: ...
def _set(self, key: int, result: Any, **kwargs) -> None: ...
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cachefunc-0.2.5.tar.gz
(3.1 kB
view hashes)
Built Distribution
Close
Hashes for cachefunc-0.2.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24fd028d2a208d889460082a8d93683547335aa854e2bdd7d9c8818f0f814d28 |
|
MD5 | fdcb95378e6940ce88dbd5b9c36e6386 |
|
BLAKE2b-256 | 33c6d898fa0288ed75f1fbe4282cc94f96317aa376fa4de45ca701acfdc318ca |