Skip to main content

A lightweight in-memory caching library with disk persistence. Supports both sync and async functions.

Project description

CacheDisk

CacheDisk is a lightweight, in-memory caching library designed for Python, offering seamless disk persistence. It provides a drop-in optimization for your frequently used sync/async functions, allowing you to cache their results and retrieve them from memory on subsequent calls.

Features

  • Easy-to-use decorators to cache the results of functions, both synchronously and asynchronously.
  • Disk persistence allows your cache to survive across application restarts, enhancing data retrieval times for repeated operations.
  • Flexible storage options with support for JSON and Pickle, choose according to your compatibility or performance needs.
  • Culling functionality to remove infrequently used cache entries, ensuring that your cache does not grow unbounded.
  • Configurable caching parameters allowing fine-tuned performance to match the needs of your application, by avoiding slow-downs caused by disk writes.

Installation

CacheDisk is available on PyPi and can be installed using pip:

pip install cachedisk

Quick Start

Basic Caching Example

Here's a quick example to get you started with CacheDisk:

from cachedisk import CacheDisk, CacheDiskConfig

# Optional: Configure CacheDisk to use JSON for serialization
CacheDiskConfig.use_json = True

@CacheDisk.sync_disk_cache()
def expensive_function(param1):
    # Simulate an expensive or time-consuming operation
    return some_expensive_computation(param1)

# First call will compute and cache the result
result = expensive_function('some_input')

# Subsequent calls with the same input will fetch the result from the cache
cached_result = expensive_function('some_input')

For asynchronous functions, simply use the @CacheDisk.async_disk_cache() decorator in a similar fashion.

Configuring the Cache Directory

By default, CacheDisk uses a directory named cache_data in your current working directory. You can customize the cache directory as follows:

CacheDiskConfig.cache_dir = '/path/to/your/custom/cache/directory'

Committing Cache Changes

To manually trigger saving all pending cache changes to disk:

CacheDisk.commit()

This can be particularly useful before application shutdown or after a batch of operations.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue if you have feedback, ideas, or code improvements.

License

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

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

CacheDisk-0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

CacheDisk-0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file CacheDisk-0.1.tar.gz.

File metadata

  • Download URL: CacheDisk-0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.4

File hashes

Hashes for CacheDisk-0.1.tar.gz
Algorithm Hash digest
SHA256 d26507921f6db2fc6aca23b64f6dc099c2d594375124a0bb3b657aa15aa69a14
MD5 afc215c167b5be8ea1387fb48ca7b345
BLAKE2b-256 5bcd8fbe281a0e0f664c99e511a8981fade234b611e41dd4a6add381ee49d28e

See more details on using hashes here.

File details

Details for the file CacheDisk-0.1-py3-none-any.whl.

File metadata

  • Download URL: CacheDisk-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.4

File hashes

Hashes for CacheDisk-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a201cb948eea9a81a2bce42cd31f27772537f93da1521be7eb7438bdc85a8730
MD5 4dab888187861bb66ad4861e4f71d87d
BLAKE2b-256 0f3f6ad625ec34e414a47a5f41ec2e9fd2bfe7abf7bf32ab9c7369f0a7fc0242

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