Skip to main content

Shared Redis cache decorator for internal Python services — write once, import everywhere.

Project description

orion-cache

Shared Redis cache decorator for Python services — write once, import everywhere.

PyPI version Python License: MIT


What is it?

orion-cache is a lightweight Python library that wraps Redis caching behind a simple decorator. Slap @redis_cache() on any function and its return value is automatically cached in Redis — no boilerplate, no repeated logic across services.

Built for multiple services that query the same database and want to share cached results without duplicating caching code.


Installation

pip install orion-cache

Quick Start

from orion_cache import redis_cache, clear_all_caches

@redis_cache(ttl=300)
def get_orders(customer):
    return db.query("SELECT * FROM orders WHERE customer = ?", customer)

@redis_cache()  # uses default TTL (300s)
def get_all_products():
    return db.query("SELECT * FROM products")

First call hits the database and caches the result. Every call after that is served from Redis until the TTL expires.


Clear Cache

# clear everything
clear_all_caches()

# clear only keys matching a pattern
clear_all_caches(pattern="get_orders:*")

Configuration

orion-cache is configured via environment variables. Create a .env file in your project root or set them in your environment directly.

Variable Default Description
REDIS_HOST localhost Redis server hostname
REDIS_PORT 6379 Redis server port
REDIS_PASSWORD (none) Redis password
REDIS_DB 0 Redis database index
CACHE_DEFAULT_TTL 300 Default TTL in seconds
REDIS_URL (auto-built) Full Redis URL — overrides all above if set

.env example:

REDIS_HOST=your-redis-host
REDIS_PORT=6379
REDIS_PASSWORD=secret
CACHE_DEFAULT_TTL=300

Or use a full URL:

REDIS_URL=redis://:secret@your-redis-host:6379/0

In production (Docker, k8s), set real environment variables instead of a .env file — the library will pick them up automatically.


Sharing Cache Across Services

If multiple services point at the same Redis instance and use the same function names, they will automatically share cached results — no extra configuration needed.

If two services have different functions with the same name that do different things, use descriptive function names to avoid collisions:

# good
def orders_get_all(): ...
def products_get_all(): ...

# risky if shared Redis
def get_all(): ...

Behavior

  • Cache miss: calls the real function, stores result in Redis, returns result
  • Cache hit: returns cached result directly, function is never called
  • Redis down: fails silently, falls through to the real function — your app keeps working
  • Non-serializable types (e.g. datetime, UUID): automatically converted to string via default=str

Requirements

  • Python 3.9+
  • Redis 5+

Contributing

Contributions are welcome!

  1. Fork the repo and clone it locally
  2. Create a virtual environment and install in editable mode:
    python -m venv .venv
    source .venv/bin/activate
    pip install -e ".[dev]"
    
  3. Make your changes
  4. Run tests before submitting:
    pytest
    
  5. Open a pull request against main

Please keep PRs focused — one feature or fix per PR.

For bugs or feature requests, open an issue on GitHub.


License

MIT © Ali Rashidi

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

orion_cache-0.1.7.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

orion_cache-0.1.7-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file orion_cache-0.1.7.tar.gz.

File metadata

  • Download URL: orion_cache-0.1.7.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for orion_cache-0.1.7.tar.gz
Algorithm Hash digest
SHA256 41860d38e82abfa420931482c8d5d403e2ab5f99f288f64fa4e74918f29156da
MD5 f8e561b269b9baf50c40464c49446e35
BLAKE2b-256 60387d79d7eed242a3f3a182234a91baf27a30afa8ad642f1b72d6082ae58abc

See more details on using hashes here.

Provenance

The following attestation bundles were made for orion_cache-0.1.7.tar.gz:

Publisher: python-publish.yml on aliinreallife/orion-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file orion_cache-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: orion_cache-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for orion_cache-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f974096e9cc92a357ccf1e1cd4c36508f37c14d15bc6d688812af6da6a97eda4
MD5 55e4d23fb22e00a01cbc13297aa7b06a
BLAKE2b-256 66cfa9780c3ea4874399380c8bc2a7160c0bec7f8c1bf6b6d775583af344291e

See more details on using hashes here.

Provenance

The following attestation bundles were made for orion_cache-0.1.7-py3-none-any.whl:

Publisher: python-publish.yml on aliinreallife/orion-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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