Skip to main content

A Python library for disk-based function caching

Project description

MatrixDiskCache

MatrixDiskCache is a lightweight Python library designed to cache function results to disk. It ensures that the results of expensive computations are saved locally, enabling reuse between multiple program executions. With support for caching complex data structures like NumPy arrays and pandas Series/DataFrames, it offers robust functionality for modern data-intensive applications.

Features

  • Persistent Caching: Cache function results to disk to reuse them across program runs.
  • Support for Complex Data: Handles numpy.ndarray, pandas.Series, and pandas.DataFrame objects seamlessly.
  • Customizable Cache Size: Set a maximum size for the cache directory to limit storage usage.
  • Easy to Use: Decorate your functions with @cache to enable caching immediately.

Installation

You can install MatrixDiskCache via pip (soon to be available on PyPI):

pip install matrix-disk-cache

Quickstart

Here is an example demonstrating how to use MatrixDiskCache:

from matrix_disk_cache import MatrixDiskCache

# Initialize the cache with an optional maxsize
cache = MatrixDiskCache(cache_dir="my_cache", maxsize=100)

@cache.cache
def expensive_computation(x, y):
    print("Computing...")
    return x + y

# First call computes and caches the result
result = expensive_computation(2, 3)  # Output: Computing...
print(result)  # Output: 5

# Second call retrieves the result from cache
result = expensive_computation(2, 3)  # No "Computing..." this time
print(result)  # Output: 5

Advanced Usage

Caching Complex Data

MatrixDiskCache supports caching of complex data types such as NumPy arrays and pandas Series/DataFrames. These are serialized into a hashable format to ensure uniqueness.

import numpy as np
import pandas as pd

@cache.cache
def process_data(array, series):
    return array.mean() + series.sum()

arr = np.array([1, 2, 3])
ser = pd.Series([4, 5, 6])

# Compute and cache the result
result = process_data(arr, ser)

# Fetch the cached result
result = process_data(arr, ser)

Limiting Cache Size

Set a maximum number of cached results using the maxsize parameter. Oldest files are deleted when the limit is exceeded:

cache = MatrixDiskCache(cache_dir="limited_cache", maxsize=50)

API Reference

MatrixDiskCache

Initialization

MatrixDiskCache(cache_dir: str = ".matrix_cache", maxsize: int = None)
  • cache_dir: Directory to store cached results (default: .matrix_cache).
  • maxsize: Maximum number of cache files (default: None, unlimited).

Methods

  • cache(func): Decorator to enable caching for the given function. Results are cached based on the function name and its arguments.

Testing

To run tests:

pytest tests

Contributing

Contributions are welcome! If you have ideas for new features or improvements, please open an issue or submit a pull request.


License

MatrixDiskCache is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

Inspired by functools.lru_cache, with an emphasis on persistent disk caching and support for data science workflows.

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

matrix_disk_cache-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

matrix_disk_cache-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file matrix_disk_cache-0.1.0.tar.gz.

File metadata

  • Download URL: matrix_disk_cache-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.20

File hashes

Hashes for matrix_disk_cache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 29be85c7601c8e6bf4df53991036772fcadff527de4c90fdbe997c7812bfe170
MD5 640fb336a70483a948d2e5e36896b4ae
BLAKE2b-256 9e2e846e33d4a3caa0edc72974167d98834ba0db8c358fec52465b3c80bae25a

See more details on using hashes here.

File details

Details for the file matrix_disk_cache-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for matrix_disk_cache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea579eb4008b0d4400e9ef7f0af1411fbf8ac1fc6a9f55dc5a4c6bf7f9604a17
MD5 73a2fe8d17fdc7ebe66b3e46510c5a72
BLAKE2b-256 64ad1dd4e0712a3c63d6535ad09dc21d598d3d09892705c53d9361749f5e545d

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