Skip to main content

Caching library for Python with sync and async support

Project description

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


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

flux_cache-0.4.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

flux_cache-0.4.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file flux_cache-0.4.0.tar.gz.

File metadata

  • Download URL: flux_cache-0.4.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for flux_cache-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b1f7251b991ecc221d4f4ab8ea0a3df215cfe591dc1fdd26830bcd2a0a75f94e
MD5 d9aed9c17913c213fa349e45d67ad572
BLAKE2b-256 fd17dc85c1576bb54d5aa6dd4f28b49ba9337f829511b987f1dc58c76df90985

See more details on using hashes here.

File details

Details for the file flux_cache-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: flux_cache-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for flux_cache-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2939835cdf9a2d75c5ede335ab546cd933c7cb4e11d972f367f742e7ab03de3f
MD5 871b4ce454498f5b9c240ca5d79288dc
BLAKE2b-256 0a06cd604bfc2016871e301c6fdc4b07509994a57a3e329790a37fd538838c6b

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