locache - a local and persistent cache for Python
locache is a single-file, 0-dependency utility package for caching the results of deterministic and pure function calls to disk, exposed via the @persist decorator.
These caches are:
- persistent across separate Python sessions: run the function once to cache the result to disk, and then hit the cache again and again in different runs/notebooks/scripts/sessions
- aware of the function's source code: if the function's source code changes, new values will be cached. If you change the source code back, the old values will still be in the cache (useful for using different versions of a function on e.g. different branches)
- configurable with
max_entriesandmax_ageparameters: control how many entries to store, and how long to keep them in the cache for.
Use-case
Have you ever written something like this?
# result = expensive_function(X, y)
result = np.load("cached_result.npy")
This is somewhat dangerous: what if you have changed expensive_function and forgotten to update the cache? Or accidentally deleted your cache file? Or you have result = np.load("cached_result-v3-final.npy")?
locache.persist is an easy way to avoid these issues:
- wrap
expensive_functionwith@persist - call
expensive_functionas usual locacheautomatically caches the result ofexpensive_functionto disk. If the function's source code changes, new values will be cached.
Installation
locache is simple enough that you could have written it yourself. To use it in your project, either:
- copy the
locache.pyfile into your project - install it with
pip install locache
API
locache provides 3 importable functions:
- the
@persistdecorator resetverbose
@persist / @persist(max_entries=-1, max_age=365)
Wrap a pure function with @persist in order to cache its results to disk. The only stipulation is that the function's arguments and return value must be pickle-able.
>>> from locache import persist
>>> @persist
... def my_func(x, num=3):
... print("Hi from foo!")
... return x * num
>>> my_func(1) # function behaviour is unchanged
Hi from foo!
3
>>> my_func(2, num=2)
Hi from foo!
4
>>> my_func(1) # cached results are returned
3
By default, @persist will store up to unlimited entries. Each of these will be removed 365 days after they are last accessed. These parameters can be changed by passing max_entries and max_age to @persist.
reset(func: Callable)
Reset the cache for a given function.
>>> from locache import reset
>>> @persist
... def squared(x):
... print(f"Squaring {x}")
... return x * x
>>> squared(2) # cache miss
Squaring 2
4
>>> squared(2) # cache hit
4
>>> reset(squared)
>>> squared(2) # cache miss
Squaring 2
4
verbose(yes: bool = True)
Turn on verbose, debug logging.
Metadata
Release files for locache 4.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| locache-4.0.2.tar.gz | 6.5 kB | With provenance | Details for locache-4.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| locache-4.0.2-py3-none-any.whl | Python 3 | none | any | Details for locache-4.0.2-py3-none-any.whl |
Total release size: 13.0 kB
Release files / locache-4.0.2.tar.gz
| Download URL | locache-4.0.2.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c372ddfe0983094330e7ac4c186b428a0d1cd22e1c539109910ddae5bb588971
|
|
BLAKE2b-256 checksum How to use checksums |
b6649a422ad9a00291fb8930b49e47ba0a56f38bf52792fa843e7e2f17f9cb6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 13, 2024.
Transparency logRelease files / locache-4.0.2-py3-none-any.whl
| Download URL | locache-4.0.2-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
195a34af8f3d24cf0ce56e5a81ee6bbb6af89da07f9502e0c5ca8ae6e94f9365
|
|
BLAKE2b-256 checksum How to use checksums |
bf8fdab996d563d2fd690fde3080d16763026e09ce11f5b1bad7461832d15ec3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 13, 2024.
Transparency logSecurity for locache 4.0.2
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for locache-4.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 13, 2024.
Transparency logA 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.