A decorator for wrapping any function in an expiring cache. Supports coroutines!
Project description
returncache
A very, very small Python utility library for caching the return value of a function until a given datetime has passed.
example
from time import sleep
from datetime import datetime, timedelta
from returncache import returncache
# This only has to be recomputed every ten minutes
@returncache()
def cached_square(value) -> tuple[datetime, float]:
print("squaring!!")
return datetime.now() + timedelta(minutes=10), value ** 2
# only the first call will cause the body of cached_square() to run
first = cached_square(42) # prints "squaring!!"
second = cached_square(42) # does not print anything
# wait for a bit...
sleep(60 * 15)
third = cached_square(42) # prints "squaring!!"
# by default, the internal cache is keyed by a hash of the parameters.
# this does require all parameters to be hashable.
# you can opt out of this behavior by passing keyed_by_parameters=False to @returncache()
# because the parameter is different, cached_square() _will_ run here!
fourth = cached_square(69) # prints "squaring!!"
Coroutines are also supported:
from asyncio import run
from aiohttp import ClientSession
from returncache import returncache
from datetime import datetime, timedelta
@returncache()
async def cached_network_request(url) -> bytes:
async with ClientSession() as session:
async with session.get(url) as response:
return datetime.now() + timedelta(minutes=10), await response.json()
async def main():
# only makes one network request
first = await cached_network_request("https://httpbin.org/get")
second = await cached_network_request("https://httpbin.org/get")
asyncio.run(main())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
returncache-1.0.0.tar.gz
(8.2 kB
view details)
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 returncache-1.0.0.tar.gz.
File metadata
- Download URL: returncache-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27fdf268ab851b01e5a215c6528c626d7f0bdee3a7366fd62ec7ff60c02e4423
|
|
| MD5 |
f35ce82f62c92b1dd5d318c82e832cd0
|
|
| BLAKE2b-256 |
0ba942cef64af9b9b1d57f7e729c058fac460d7cf97d495ed41446320494c89b
|
File details
Details for the file returncache-1.0.0-py3-none-any.whl.
File metadata
- Download URL: returncache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.29
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3373e54471cc857af306c95104254ea7a7033c0c8e37eb7a4ab2be0ac3fcd066
|
|
| MD5 |
6f22a382805bdcbd4a6b9f996b1313fe
|
|
| BLAKE2b-256 |
7912ff92221cd41dba7f3451cb40c2c3fc99aee83e0e222d82cf95a6d773fd9b
|