Skip to main content

thornfield: Advanced caching in python

Build Status codecov License: MIT Code style: black

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 functions.

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):
    ...

Caching abstract methods

In order to avoid adding the same decorator to all implementations of an abstract method, you can use cache_method as follows:

class Base(ABC):
    def __init__(self):
        cacher.cache_method(do_something)

    @abstractmethod
    def do_something(self):
        pass

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.

Metadata

Release files for thornfield 1.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thornfield 1.5.1
File Size Uploaded Provenance Actions
thornfield-1.5.1.tar.gz 10.8 kB Without provenance Details for thornfield-1.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thornfield 1.5.1
File Interpreter ABI Platform Actions
thornfield-1.5.1-py3-none-any.whl Python 3 none any Details for thornfield-1.5.1-py3-none-any.whl

Total release size: 25.7 kB

Release files / thornfield-1.5.1.tar.gz

Download URL thornfield-1.5.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4b371f2a7c292819b2214d5d352ab151566a35355ce03d790b99f61b1e73cdb5
BLAKE2b-256 checksum
How to use checksums
72389b53909dd05a4406eb933b7d8fda81b15cb066d661b8587067293bcebb26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Release files / thornfield-1.5.1-py3-none-any.whl

Download URL thornfield-1.5.1-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69cf086cfda1d3a9a9432e19f0a66f7108445f64e61a177f4eefa1ffd6f3c1f3
BLAKE2b-256 checksum
How to use checksums
66ddccf9f97bfc7baee16226b76c7038e3a068c82958e762d217706d638f4f7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

Security for thornfield 1.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page