Utilities to make caching data easier
Project description
cachingutils
Utilities to make caching data easier
Examples
Basic caching:
from cachingutils import cached
@cached()
def fib(n: int) -> int:
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(100)) # 354224848179261915075
Caching with your own cache object:
from cachingutils import Cache, cached
my_cache = Cache()
@cached(cache=my_cache)
def fib(n: int) -> int:
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(100)) # 354224848179261915075
Async caching:
from asyncio import run
from cachingutils import acached
@acached()
async def fib(n: int) -> int:
if n < 2:
return n
return await fib(n - 1) + await fib(n - 2)
print(run(fib(100))) # 354224848179261915075
Caching specific positional args:
from cachingutils import cached
@cached(include_posargs=[0])
async def add(a: int, b: int) -> int:
return a + b
print(add(1, 2)) # 3
print(add(2, 2)) # 3
print(add(2, 3)) # 5
Caching specific keyword args:
from cachingutils import cached
@cached(include_posargs=[0], include_kwargs=['c'])
def add(a: int, b: int, *, c: int) -> int:
return a + b
print(add(1, 2, c=3)) # 3
print(add(2, 2, c=3)) # 4
print(add(2, 3, c=3)) # 4
Caching with a timeout:
from time import sleep
from cachingutils import cached
@cached(timeout=1, include_posargs=[0])
def add(a: int, b: int) -> int:
return a + b
print(add(1, 2)) # 3
print(add(1, 3)) # 3
sleep(2)
print(add(1, 3)) # 4
Using a raw Cache
object:
from time import sleep
from cachingutils import Cache
my_cache: Cache[str, int] = Cache(timeout=5)
my_cache["abc"] = 123
print(my_cache["abc"]) # 123
sleep(6)
print(my_cache["abc"]) # KeyError: 'abc'
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
cachingutils-1.0.3.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for cachingutils-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a2071a771bdf4156b3eaac52f41d70367fdbd19f6b606f8239887dcbd5a4275 |
|
MD5 | 2d317e1dae4530eb210d7026dabeeebc |
|
BLAKE2b-256 | 19a8fb1817086b4e49f40c32cb50a2c6bb4a899c7ca2ea81ebd195a68603c502 |