Skip to main content

Cache expensive function calls to disk.

Project description

locache - a local cache for Python

PyPI PyPI - Downloads GitHub codecov

A single-file utility library for caching the results of deterministic and pure function calls to disk, exposed via the @persist decorator.

These caches are persistent across runs of the program. If changes to the function's source code are detected, the cache is reset. This can also be manually performed with the reset function.

Installation

locache is simple enough that you could have written it yourself. To use it in your project, either:

  • copy the locache.py file into your project
  • install it with pip install locache

Usage

locache provides 3 importable functions:

  • the @persist decorator
  • reset
  • verbose

@persist

Wrap a pure function with @persist 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)        
# prints "Hi from foo!", returns 3
my_func(2, num=2) 
# prints "Hi from foo!", returns 4
my_func(1)        
# returns 3

reset(func: Callable)

Reset the cache for a given function.

from locache import reset

reset(my_func)
my_func(1) # prints "Hi from foo!", returns 3

verbose(yes: bool = True)

Turn on verbose logging.

from locache import verbose

reset(my_func)
verbose(yes=True)

my_func(1) 
# prints "cache miss for squared(1)"
# prints "Hi from foo!"
# returns 3

my_func(1)
# prints "cache hit for squared(1)"
# returns 3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

locache-3.0.1.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

locache-3.0.1-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page