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())
Release files for returncache 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| returncache-1.0.0.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| returncache-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.6 kB
Release files / returncache-1.0.0.tar.gz
| Download URL | returncache-1.0.0.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27fdf268ab851b01e5a215c6528c626d7f0bdee3a7366fd62ec7ff60c02e4423
|
|
BLAKE2b-256 checksum How to use checksums |
0ba942cef64af9b9b1d57f7e729c058fac460d7cf97d495ed41446320494c89b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.29
|
Release files / returncache-1.0.0-py3-none-any.whl
| Download URL | returncache-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3373e54471cc857af306c95104254ea7a7033c0c8e37eb7a4ab2be0ac3fcd066
|
|
BLAKE2b-256 checksum How to use checksums |
7912ff92221cd41dba7f3451cb40c2c3fc99aee83e0e222d82cf95a6d773fd9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.29
|