Skip to main content

A Python library for persistent JSON-based caching of class state and function results.

Project description

Cacherator

Cacherator is a Python package that provides persistent caching functionality for your Python classes and functions. It allows you to easily cache function results to disk, improving performance for expensive computations or API calls.

Features

  • Persistent caching of function results
  • Customizable Time-To-Live (TTL) for cached data
  • Option to clear cache on demand
  • JSON-based storage for easy inspection and portability
  • Automatic serialization and deserialization of cached data
  • Support for instance methods and properties

Installation

You can install PyCacherator using pip:

pip install cacherator

Usage

Basic Usage

To use Cacherator, simply inherit from the JSONCache class and use the @Cached decorator on your methods:

from cacherator import JSONCache, Cached

class MyClass(JSONCache):
    def __init__(self, data_id):
        super().__init__(data_id=data_id)

    @Cached()
    def expensive_operation(self, arg1, arg2):
        # Your expensive computation here
        return result

Customizing Cache Behavior

You can customize the caching behavior by passing arguments to the JSONCache constructor and the @Cached decorator:

from datetime import timedelta
from cacherator import JSONCache, Cached

class MyClass(JSONCache):
    def __init__(self, data_id):
        super().__init__(
            data_id=data_id,
            directory="custom/cache/dir",
            clear_cache=False,
            ttl=timedelta(days=999),
            logging=True
        )

    @Cached(ttl=300, clear_cache=False)
    def cached_method(self, arg):
        # Method implementation
        return result

API Reference

JSONCache

The base class for objects with caching capabilities.

Parameters:

  • data_id (str): Unique identifier for the cache instance.
  • directory (str): Directory to store cache files (default: "json/data").
  • clear_cache (bool): Whether to clear existing cache on instantiation (default: False).
  • ttl (timedelta | int | float): Default Time-To-Live for cached data (default: 999 days).
  • logging (bool): Enable logging of cache operations (default: True).

@Cached

Decorator for caching method results.

Parameters:

  • ttl (float | int | timedelta): Time-To-Live for the cached result (defaults to the ttl set on object level).
  • clear_cache (bool): Whether to clear existing cache before execution (default: False).

License

This project is licensed under the MIT License.

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

cacherator-1.0.6.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

cacherator-1.0.6-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file cacherator-1.0.6.tar.gz.

File metadata

  • Download URL: cacherator-1.0.6.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for cacherator-1.0.6.tar.gz
Algorithm Hash digest
SHA256 dd0cbf4b9aedc2d03a01e0a8a1982030fb359c0f1e59f73f0f44f3fe4c53ad02
MD5 1fa2fa8d93528819e6990d5415ceba8c
BLAKE2b-256 0d4a1b7d659493fb4dbbe9b2da287dd733ed9e2206e4f42706999c1c63a25341

See more details on using hashes here.

File details

Details for the file cacherator-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: cacherator-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for cacherator-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ed7b736b54b0e93b636959dad4812edde6f7c1521d5b5e9fe4b775ed5e887a80
MD5 c83428a3bb58d7cdfa1f695d5f0f688e
BLAKE2b-256 f5d986e492faa159cb93a190c44391b4759fd76c81beb55c3bdf0e2dca3caec3

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