Caching functions and coroutines result with decorators
This project has been archived.
The maintainers of this project have marked this project as archived. No new releases are expected.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cachefunc-0.2.6.tar.gz.
File metadata
- Download URL: cachefunc-0.2.6.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4977384a8c3a05d2458eab3af404b72611363a0c597016f8374eccca89e4063e
|
|
| MD5 |
2f043eaf881f16e9c8c28aaf991bd7f3
|
|
| BLAKE2b-256 |
c1d086e40b9d94a2f3530b80fedba6b3a79b0d70704f4691e3d6521f4bef31b3
|
File details
Details for the file cachefunc-0.2.6-py3-none-any.whl.
File metadata
- Download URL: cachefunc-0.2.6-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Darwin/22.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c359bea716dad21acf7d9af0c68d57fb562b3f69d2119891c9983b4d507621f4
|
|
| MD5 |
f07b1c664bb4f847b92e74e5d4faac3b
|
|
| BLAKE2b-256 |
2b13a2535d098803b393e4627b4c2d8dc981f127e5f9e7972e80d0effc2ac7d7
|