Skip to main content

Caching library for Python with sync and async support

Project description

Flux Cache

An extensible caching library for Python with support for synchronous and asynchronous functions.

Installation

pip install flux-cache[redis]

Quick Start

Basic Usage

from flux_cache import cache

@cache(ttl=60)
def expensive_function(x, y):
    print("Computing...")
    return x + y

expensive_function(1, 2)  # computes
expensive_function(1, 2)  # cached

Async Usage

from flux_cache import async_cache

@async_cache(ttl=60)
async def fetch_data(x):
    return x * 2

How It Works

  • A cache key is generated using:

    • Function module
    • Function name
    • Arguments (args + kwargs)
  • The key is hashed using SHA-256

  • Values are stored in the backend along with optional expiration timestamps


Cache Control

Clear entire cache

expensive_function.clear()

Invalidate specific call

expensive_function.invalidate(1, 2)

Backends

Default: In-Memory

Thread-safe, simple dictionary-based backend.

from flux_cache.backends import MemoryBackend

@cache(backend=MemoryBackend())
def foo():
    return "bar"

Custom Backend

Implement the BaseBackend interface:

class BaseBackend:
    def get(self, key): ...
    def set(self, key, value, ttl=None): ...
    def delete(self, key): ...
    def clear(self): ...

🧪 Testing

Run tests using:

pytest

📄 License

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

flux_cache-0.2.0.tar.gz (6.5 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.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flux_cache-0.2.0.tar.gz
  • Upload date:
  • Size: 6.5 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.2.0.tar.gz
Algorithm Hash digest
SHA256 623bfbb6a1e9476b6f5a13be0bcd016c63e94782c95b6a4f9ec6c3f4c8fbe9e1
MD5 edca2fc0514b7082fa9f3cde239e529b
BLAKE2b-256 0ade15f5c6df92d776f99afbdc08f81dffd61370863c68c807dff7b92a37398f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flux_cache-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48e4756dffa7a3f5abb67ccb41d2ec33b3e60a252f6f57264305dea9c24f4681
MD5 80209e62ccf3dd76d9742ce01053114d
BLAKE2b-256 e50e7bc06aaae16f4f906fa3f9904f8bfcb39bbcc8e7bd636128f513355e2f9a

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