Skip to main content

Cache function results to disk

Project description

Function Cacher

Documentation Status Test Status

Overview

The Function Cacher project provides a way to cache the result of a (e.g. computation or IO-heavy) function to a location on the filesystem, ideally a tmpfs.

In comparison to Python’s integrated functools.cache, this approach provides caching outside of a programs lifetime. The (originally) intended use-case is caching results of long-running database queries during rapid-prototyping (& rapidly crashing) data analysis scripts.

Please refer to https://function-cacher.readthedocs.io for documentation, more examples, and further details.

Tested on Python 3.10 and upwards.

Installation

Use pip to install this package:

pip install functionCacher

The package is located here: https://pypi.org/project/functionCacher/

Basic Usage

from functionCacher.Cacher import Cacher
cacher_instance = Cacher()

@cacher_instance.cache
def myfunc(*args):
	print(f"myfunc is getting called with {args}")
	return args

print(myfunc(1,2,3)) # cache miss; prints "myfunc is getting..."
print(myfunc(1,2,3)) # cache hit ; prints nothing
print(myfunc(2,3,4)) # cache miss; prints "myfunc is getting..."

print(f"Cache path is: {cacher_instance.cachePath}")

Output:

% python3 basic_example.py
myfunc is getting called with (1, 2, 3)
(1, 2, 3)
(1, 2, 3)
myfunc is getting called with (2, 3, 4)
(2, 3, 4)
Cache path is: /path/to/cachedir

Features

  • cache function results to disk
  • pickles objects for storage
  • cache compression (LZMA, ZSTD)
  • HMAC-verification of cache file (do not forget to set a proper hmac_key during Cacher initialisation!)
  • cache decorator
  • automatic cache ID generation, based on
    • function parameter
    • function name
    • function source code

Notes & Caveats

  1. Caches are on the filesystem. For maximum performance, you might want to consider a RAM-backed FS like a ramdisk or tmpfs.

  2. FunctionCacher assumes pure functions without side-effects. It can only discern two cache-entries via the function parameters!

  3. Python pickle might mess with your cached function results. Works well in most cases, though.

Related Projects

While this project has been developed largely in a vacuum (sorry! Didn't know better), several related projects approach result caching in Python in similar or identical ways:

Licence

MIT Licence

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

functionCacher-1.0.5.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

functionCacher-1.0.5.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file functionCacher-1.0.5.2.tar.gz.

File metadata

  • Download URL: functionCacher-1.0.5.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for functionCacher-1.0.5.2.tar.gz
Algorithm Hash digest
SHA256 ecfdbb55e37903a50a70820329339623efc26ae5c02ad6405bd469b59ba17ef2
MD5 56b5d67a8591f36c8420595f7d768125
BLAKE2b-256 9cd5f774bfe9ea3ff2efaeecf04e3976f1a1ae1e34b5c1291fe540f5af99b062

See more details on using hashes here.

File details

Details for the file functionCacher-1.0.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for functionCacher-1.0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b46c58a2ebe52031c0fab315eb6360c739426d3860679d1717494a9bbcd872ef
MD5 abf6c8c7308cf7055e2fc6709eedfc9c
BLAKE2b-256 9ac346322e58a65b4c7b884a833f1306a64fe9ffc79bb342ed64ca01345f39f4

See more details on using hashes here.

Supported by

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