Cache any python object to file using improved pickling


Keywords
cache, caching, file, pickle, dill
License
Apache-2.0
Install
pip install anycache==2.1.0

Documentation

https://img.shields.io/pypi/dm/anycache.svg?label=pypi%20downloads https://readthedocs.org/projects/anycache/badge/?version=latest https://readthedocs.org/projects/anycache/badge/?version=2.1.0 https://img.shields.io/badge/linter-pylint-%231674b1?style=flat https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square

To cache the result of a function, use the global unlimited anycache:

>>> from anycache import anycache
>>> @anycache()
... def myfunc(posarg, kwarg=3):
...     print("  Calcing %r + %r = %r" % (posarg, kwarg, posarg + kwarg))
...     return posarg + kwarg
>>> myfunc(8, 5)
  Calcing 8 + 5 = 13
13
>>> myfunc(8, 5)
13

anycache caches nearly any python object. Also lambda statements. It uses Dill as backend. An improved version of pythons build-in pickle.

To preserve the result between multiple python runs, a persistent cache directory needs to be set.

>>> from anycache import anycache
>>> @anycache(cachedir='/tmp/anycache.my')
... def myfunc(posarg, kwarg=3):
...     return posarg + kwarg

The AnyCache object serves additional functions for cache clearing and size handling.

Installation

To install the anycache module run:

pip install anycache

If you do not have write-permissions to the python installation, try:

pip install anycache --user