Skip to main content

fleche

docs

A persistent caching solution for arbitrary Python functions - like lru_cache on steroids.

flèche is French for 'arrow' and also used in fencing as a fast attack.

Features

  • Persistent Caching: Cache function results across runs using various storage backends
  • Flexible Storage: Choose from file-based, SQL, in-memory, or custom storage solutions
  • Intelligent Hashing: Automatically generates cache keys from function arguments
  • Query Support: Search and retrieve cached results with metadata filtering
  • Configurable: Control what gets hashed (version, module, code, arguments)
  • Multiple Backends: File (pickle, cloudpickle, dill), SQLAlchemy, Bagofholding, and more
  • Thread-Safe: Safe for use in multi-threaded environments
  • Type-Aware: Works seamlessly with NumPy, Pandas, and custom types

Installation

Basic Installation

pip install fleche

With conda

fleche is available on conda-forge. Two packages are published:

  • fleche-base — the core library only (no optional dependencies)
  • fleche — the full install, which also pulls in the optional dependencies (cloudpickle, dill, sqlalchemy, and bagofholding), enabling the SQL, SSH, alternate-serialization, and Bagofholding features out of the box
# Core library only
conda install -c conda-forge fleche-base

# Full install with all optional dependencies
conda install -c conda-forge fleche

With Optional Dependencies

# For SQL storage
pip install "fleche[sqlalchemy]"

# For alternate serialization formats
pip install "fleche[cloudpickle,dill]"

# For Bagofholding storage
pip install "fleche[bagofholding]"

# For SshCache (sharing caches across machines over SSH) — requires cloudpickle
pip install "fleche[ssh]"

# For running cached calls through executorlib executors
pip install "fleche[executorlib]"

# For documentation
pip install "fleche[docs]"

# For development and testing
pip install "fleche[tests]"

See the Optional dependencies section of the docs for a full table of extras and the features they enable.

Quick Start

Basic Usage

from fleche import fleche

@fleche()
def expensive_function(x, y):
    """This function's results will be cached."""
    print(f"Computing for {x}, {y}...")
    return x + y

# First call computes the result
result = expensive_function(1, 2)  # prints "Computing for 1, 2..."

# Second call retrieves from cache
result = expensive_function(1, 2)  # no print - result from cache!

# Different arguments compute again
result = expensive_function(2, 3)  # prints "Computing for 2, 3..."

Working with Cache Results

@fleche()
def compute(x):
    return x ** 2

# Get the cache key (digest) for arguments
digest = compute.digest(5)

# Check if result is cached
if compute.contains(5):
    result = compute.load(5)
else:
    result = compute(5)

# Get Call object with metadata
call = compute.call(5)

Configuration

Decorator Options

@fleche(
    version=1,           # Versioning for function changes
    hash_version=True,   # Include version in cache key
    hash_module=True,    # Include module name in cache key
    hash_code=False,     # Include function code in cache key
    require=None,        # Required argument for caching
    ignore=None,         # Arguments to ignore in cache key
)
def my_function(x):
    return x * 2

Storage Backends

In-Memory Storage (Default)

  • Default when no fleche.toml configuration file is present
  • Transient: data is lost when the process exits

File Storage

  • Stores cache in filesystem using pickle (or cloudpickle/dill)
  • Persistent across runs

SQL Storage

  • Requires sqlalchemy
  • Call storage only — stores call records (function name, arguments, metadata) in a SQL database; a separate value backend (file or memory) is still required for results
  • SQLite, PostgreSQL, and MySQL/MariaDB are exercised in CI; any other SQLAlchemy-supported database should work
  • Enables efficient server-side filtering when querying cached calls

SSH (remote) Cache

  • Requires cloudpickle (pip install fleche[ssh]) — used as the wire protocol between client and remote server; not optional
  • Forwards every cache operation over a persistent ssh host python -m fleche remote --serve subprocess
  • Stack with a local cache to read-through to a shared remote one — see fleche.remote.SshCache

Custom Backends

Subclass ValueStorage/CallStorage (via StorageBackend plus ValueMixin/ CallMixin, see fleche.storage) to create custom backends.

Advanced Features

Versioning

Track function versions to invalidate cache when implementation changes:

@fleche(version=2)
def process_data(data):
    # Incrementing version invalidates all v1 cache entries
    return data * 2

Ignoring Arguments

Skip certain arguments when generating cache keys:

@fleche(ignore=['verbose', 'debug'])
def compute(x, y, verbose=False, debug=False):
    return x + y  # Cache key only uses x and y

Querying Results

Find cached results matching specific criteria (only after issuing a corresponding call):

@fleche()
def fetch_data(user_id, date):
    return {"user": user_id, "date": date}

# Issue a call to cache the result
fetch_data(user_id=123, date='2024-01-01')

# Get all cached results for fetch_data
all_results = list(fetch_data.query())

# Get results where user_id=123, any date (omitted arguments act as wildcards)
results = list(fetch_data.query(user_id=123))

# Inspect a result
for call in fetch_data.query(user_id=123):
    print(call.arguments, call.result)

Performance

Use the included benchmarks to evaluate performance:

python -m benchmarks.run_benchmarks

Testing

Run the test suite:

pip install "fleche[tests]"
pytest tests/

Contributing

Contributions are welcome! Please ensure tests pass and code follows the project style.

License

BSD-3-Clause License - see LICENSE file for details.

Resources

  • Documentation: See docs/ directory
  • Tests: See tests/ for usage examples
  • Benchmarks: See benchmarks/ for performance metrics

Release files for fleche 0.22.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fleche 0.22.0
File Size Uploaded
fleche-0.22.0.tar.gz 1.6 MB Details

Release files / fleche-0.22.0.tar.gz

Download URL fleche-0.22.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
1bfe23a1a8fbdf90f5e65ef25c2387a574dc5b9b5d16f940889eabc2389335fd
BLAKE2b-256 checksum
How to use checksums
767a5067fdff09ca342f17b0435006317a22b3132070fcb2ee4f80a7ca395ce1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 28, 2026.

Transparency log

Release history Release notifications | RSS feed

0.22.1

1 release file

This release

0.22.0 This release

1 release file

0.21.2

1 release file

0.21.1

1 release file

0.21.0

1 release file

0.20.0

1 release file

0.19.2

1 release file

0.19.1

1 release file

0.19.0

1 release file

0.18.0

1 release file

0.17.0

1 release file

0.16.1

1 release file

0.16.0

1 release file

0.15.0

1 release file

0.14.1

1 release file

0.13.1

1 release file

0.13.0

1 release file

0.12.1

1 release file

0.12.0

1 release file

0.11.0

1 release file

0.10.0

1 release file

0.9.0

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page