Cachalot


Cachalot is a minimal persistent memoization cache. It provides a decorator, that stores function result for future use. Perfect for heavy computations and I/O operation (such as web requests). On backend, it uses TinyDB for storage.
Features
- Simple usage via decorator
- Persistent caching
- Optional in-memory caching
- Key expiration
- Maximum cache size, to prevent bloat
Installation
Cachalot requires Python 3.11 or newer to run.
Python package
You can easily install Cachalot using pip:
pip3 install cachalot
Manual
Alternatively, to get the latest development version, you can clone this repository and then manually install it:
git clone git@gitlab.com:radek-sprta/cachalot.git
cd cachalot
pip install .
Usage
from cachalot import Cache
@Cache()
def expensive_function():
return expensive_calculation()
Advanced usage
from cachalot import Cache
@Cache(
path="cache.json",
timeout=3600,
size=5000,
filesize=1_000_000,
retry=True,
renew_on_read=True,
)
def expensive_function():
return expensive_calculation()
path: Path to the database file. Defaults to .cache.json. If None, the cache is kept in memory.timeout: How long should the data be cached in seconds. Defaults to 0 (infinite).size: Maximum number of keys cached. Defaults to 0 (infinite).filesize: Maximum size of database file in bytes. Defaults to 0 (infinite). Cannot be used with an in-memory cache.retry: Retry if the cached result is None. Defaults to False.renew_on_read: Renew the entry, i.e refresh the entry timestamp on reads. Defaults to True.
Caveats
- Cache keys are the function's module and qualified name plus positional and keyword arguments (keyword order irrelevant). Defaults are not applied:
f(1)andf(1, b=2)differ even if2is the default, and so do positional and keyword calls. For methods,selfis intentionally excluded, so all instances share results. - Functions with the same qualified name share a key: lambdas in the same scope, and functions made by the same factory (e.g.
make_scaler(2)andmake_scaler(3)). Closure state is not part of the key; use named top-level functions instead. - Arguments are encoded with jsonpickle, which is not canonical: sets may encode in a different order between processes, causing misses.
- When
sizeis exceeded, the least recently used entry is evicted. Use is the last read withrenew_on_read=True, otherwise the last write. - Results larger than
filesizebytes are not cached (the entry is skipped, not stored). - Cache objects on the same file within one process share one database. The file is still not locked; do not share it between concurrent processes or threads.
- Values are stored with jsonpickle; loading a cache file can execute code. Only use cache files from trusted sources.
Manually deleting entries
If you want to manually invalidate an entry, you can calculate the hash of the function call and then pass it to the remove method.
cache = Cache()
@cache
def expensive_function(arg):
return expensive_calculation(arg)
key = cache.calculate_hash(expensive_function)("arg")
cache.remove(key)
For more information, see documentation.
Contributing
For information on how to contribute to the project, please check the Contributor's Guide
Contact
incoming+radek-sprta/cachalot@gitlab.com
License
MIT License
Credits
This package was created with Cookiecutter and the python-cookiecutter project template. Inspired by Cashier
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Evan Widloski 💻 |
Saša Tomić 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Metadata
Release files for Cachalot 2.0.0
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 | |
|---|---|---|---|---|
| cachalot-2.0.0.tar.gz | 6.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cachalot-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.0 kB
Release files / cachalot-2.0.0.tar.gz
| Download URL | cachalot-2.0.0.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
585ac8f6b71456bb4bd009c7d16a90fbda29491b1938e0cf776b0e25b3b8b24d
|
|
BLAKE2b-256 checksum How to use checksums |
871c269eb75704f5412d15c28667688a9cdc19cc0f77b0df9213ff48b6172042
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / cachalot-2.0.0-py3-none-any.whl
| Download URL | cachalot-2.0.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0151f769c677ac0f0bf8dbc0f3e2bc8a229023039017287a1767836a0fb212df
|
|
BLAKE2b-256 checksum How to use checksums |
e27838b7ca27595eee3fa8c9790d2647fe2eb76b522d19f3cacc354965e50e2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for Cachalot 2.0.0
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.