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
Caching with an LRU cache:
from cachingutils import LRUCache, cached
@cached(cache=LRUCache(2, timeout=60), include_posargs=[0])
def fetch_thing(thing_id: int, thing_name: str) -> tuple[int, str]:
return thing_id, thing_name # Imagine this is a call to an API
print(fetch_thing(123, "456")) # (123, "456")
print(fetch_thing(123, "789")) # (123, "456")
fetch_thing(567, "789")
fetch_thing(789, "456")
print(fetch_thing(123, "456")) # Cache miss
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'
All of the above decorators also work within classes:
from cachingutils import cached
class MyClass:
@cached()
def fib(self, n: int) -> int:
if n < 2:
return n
return self.fib(n - 1) + self.fib(n - 2)
my_class = MyClass()
print(my_class.fib(100)) # 354224848179261915075
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 cachingutils-1.0.7.tar.gz.
File metadata
- Download URL: cachingutils-1.0.7.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.16.0-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3229f9a600aed1666d45e1dd1bf09c64616dbe67db68df79a8abdf5bee1d3b5
|
|
| MD5 |
a612170aab85efa991404ed73dd471c1
|
|
| BLAKE2b-256 |
78ae4df0fc648d06d24717ede448c40567a54dd8fabe1f06fdbb7e5f453e6fc4
|
File details
Details for the file cachingutils-1.0.7-py3-none-any.whl.
File metadata
- Download URL: cachingutils-1.0.7-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.16.0-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84b18d689c324c326037ed9574c97e49cc456c68006828fef31314aff49c970c
|
|
| MD5 |
2de115f8fdb35c3c620500e838c6ed86
|
|
| BLAKE2b-256 |
6c41beedc3d7dbe191b991049415962be012d776bd51697d879d963f7571716c
|