Minimal persistent memoization cache
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.
- Simple usage via decorator
- Persistent caching
- Key expiration
- Maximum cache size, to prevent bloat
Cachalot requires Python 3.5 or newer to run.
You can easily install Cachalot using pip:
pip3 install cachalot
Alternatively, to get the latest development version, you can clone this repository and then manually install it:
git clone email@example.com:radek-sprta/cachalot.git cd cachalot python3 setup.py install
from cachalot import Cache @Cache() def expensive_function(): return expensive_calculation()
from cachalot import Cache @Cache(path='cache.json', timeout=3600, size=5e3, filesize=1e6, retry=True, renew_on_read=True) def expensive_function(): return expensive_calculation()
path: Path to the database file. Defaults to .cache.json.
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).
retry: Retry if result is blank. Defaults to False.
renew_on_read: Renew the entry, i.e refresh the entry timestamp on reads. Defaults to True
Manually deleting entries
If you want to manually invalidate an entry, you can calculate the hash of the function call and then pass it the
key = cache.calculate_hash(len)('teststring') cache.remove(key)
For more information, see documentation.
For information on how to contribute to the project, please check the Contributor's Guide
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.