Advanced caching in python
Project description
thornfield
: Advanced caching in python
Installation
pip install thornfield
Usage
Choose the cache storage you want to use - in-memory, redis and postgresql are currently implemented.
You can use a different storage by implementing the Cache
interface.
Then, use the cached
decorator to annotate the function being cached:
cacher = Cacher(cache_factory_func)
@cacher.cached
def foo():
...
The decorator supports:
- Setting an expiration time for the cached values.
- Caching only values that match a constraint (e.g. not
None
). - Using only some of the function parameters as keys for the cache.
- Caching async function.
Caching only some parameters
In case you don't want to use all the parameters of the function as cache key,
you can the Cached
or NotCached
types:
from thornfield.typing import Cached, NotCached
@cached
def request(url: str, token: str, timeout: NotCached[int]):
...
@cached
async def request_async(url: Cached[str], timeout: int, callback):
...
Cache Factories
In the cache_factories
package you can find cache factories for Redis and PostgreSQL.
They both cache each function to a different table (in PostgreSQL, db in Redis).
Their create
method can be passed as cache_impl
to the constructor of Cacher
.
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
Built Distribution
Hashes for thornfield-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c984d16994f2226398bd7a59c04e67f25dd5f5917232d8c761a882a410b9b831 |
|
MD5 | 8bd1a8e97fa255e5b5037516d54e8afd |
|
BLAKE2b-256 | 42d67438ff754b622c9fe9c3204a3a1f71e7e67e219afa8fa6948ff1a75d5744 |