Cachalot

Cachalot is a minimal persistent memoization cache, that uses TinyDB as backend.

Active Pure Nim score 65/100 · last commit 2026-10-31 · 2 stars · tests present · no docs generated

Summary

Latest Version Unknown
License Unknown
CI Status Failing
Stars 2
Forks 2
Open Issues 0
Last Commit 2026-10-31
Downloads 0
Last Indexed 2026-11-23 09:07

Installation

nimble install Cachalot
choosenim install Cachalot
git clone https://gitlab.com/radek-sprta/Cachalot

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
Cachalot ✓ ✓ ✓ - - - - - - -

Source

Repository https://gitlab.com/radek-sprta/Cachalot
Homepage https://gitlab.com/radek-sprta/Cachalot
Registry Source gitlab

README

Cachalot PyPI version Pipeline status Coverage reportDownloads

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) and f(1, b=2) differ even if 2 is the default, and so do positional and keyword calls. For methods, self is 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) and make_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 size is exceeded, the least recently used entry is evicted. Use is the last read with renew_on_read=True, otherwise the last write.
  • Results larger than filesize bytes 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

mail@radeksprta.eu

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!