Skip to main content

Flux Cache

An extensible, and production-ready caching library for Python with built-in stampede protection, async support, and pluggable backends.


Installation

pip install flux-cache[redis]

Quick Start

Basic Usage

from flux_cache import cache

@cache(ttl=60)
def get_data(x):
    print("Computing...")
    return x * 2

get_data(2)  # computes
get_data(2)  # cached

Async Support

import asyncio
from flux_cache import cache

@cache(ttl=60)
async def fetch_data(x):
    print("Fetching...")
    await asyncio.sleep(1)
    return x * 2

asyncio.run(fetch_data(2))

Backends

Memory Backend (Default)

from flux_cache import cache
from flux_cache.backends import MemoryBackend

@cache(backend=MemoryBackend(), ttl=60)
def func():
    return "data"

Redis Backend

from flux_cache import cache
from flux_cache.backends import RedisBackend

backend = RedisBackend(host="localhost", port=6379)

@cache(backend=backend, ttl=60)
def func():
    return "data"

File Backend

from flux_cache import cache
from flux_cache.backends import FileBackend

backend = FileBackend(directory="/tmp/flux_cache")

@cache(backend=backend, ttl=60)
def func():
    return "data"

Stampede Protection

Flux Cache prevents multiple concurrent calls from recomputing the same value.

How it works:

  • First request acquires a lock
  • Other requests wait
  • Cached value is reused after computation

Works for:

  • Threads (sync)
  • Async coroutines

Cache Stats

Each cached function tracks performance:

@cache
def slow():
    return "done"

slow()
slow()

print(slow.stats.hits)   # 1
print(slow.stats.misses) # 1

Cache Management

Invalidate Specific Entry

slow.invalidate()
slow.invalidate(1, 2, key="value")

Clear Entire Cache

slow.clear()

Cache Key Generation

Flux Cache automatically generates keys using:

  • Function name
  • Module
  • Arguments (args + kwargs)

This ensures:

  • Deterministic caching
  • No collisions

Running Tests

pytest

Extending Flux Cache

Custom Backend

from flux_cache.backends.base import BaseBackend

class CustomBackend(BaseBackend):
    def get(self, key):
        ...

    def set(self, key, value, ttl=None):
        ...

    def delete(self, key):
        ...

    def clear(self):
        ...

Custom Serializer

from flux_cache.serializers.base import BaseSerializer

class CustomSerializer(BaseSerializer):
    def dumps(self, value):
        ...

    def loads(self, value):
        ...

License

MIT License


Author

Alvin Shaita


Metadata

Release files for flux-cache 0.4.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 flux-cache 0.4.0
File Size Uploaded Provenance Actions
flux_cache-0.4.0.tar.gz 7.7 kB Without provenance Details for flux_cache-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flux-cache 0.4.0
File Interpreter ABI Platform Actions
flux_cache-0.4.0-py3-none-any.whl Python 3 none any Details for flux_cache-0.4.0-py3-none-any.whl

Total release size: 17.8 kB

Release files / flux_cache-0.4.0.tar.gz

Download URL flux_cache-0.4.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b1f7251b991ecc221d4f4ab8ea0a3df215cfe591dc1fdd26830bcd2a0a75f94e
BLAKE2b-256 checksum
How to use checksums
fd17dc85c1576bb54d5aa6dd4f28b49ba9337f829511b987f1dc58c76df90985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / flux_cache-0.4.0-py3-none-any.whl

Download URL flux_cache-0.4.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2939835cdf9a2d75c5ede335ab546cd933c7cb4e11d972f367f742e7ab03de3f
BLAKE2b-256 checksum
How to use checksums
0a06cd604bfc2016871e301c6fdc4b07509994a57a3e329790a37fd538838c6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Security for flux-cache 0.4.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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