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.3.tar.gz (5.5 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.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: orion_cache-0.1.3.tar.gz
  • Upload date:
  • Size: 5.5 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.3.tar.gz
Algorithm Hash digest
SHA256 7e1bde9f77156ee1a47d1020bdbd8c54406bb53edd72c1b5f1b3b0518f32cd3d
MD5 617eb3ae56e731d21b639ad19c2592ff
BLAKE2b-256 fcd730b74f920a36d776cd2e28060476c75f2cfc34f46408a0cd8074f3875696

See more details on using hashes here.

Provenance

The following attestation bundles were made for orion_cache-0.1.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: orion_cache-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 00603c0daed62ec83fc2e121ba9022092441e3db7ff342875cd17f4b7f24cd0a
MD5 007e72757f483c975f714887ddaba10a
BLAKE2b-256 310790937091090dc1c0d645b8d643322246f31eae1bf76cd6095a9db1594c25

See more details on using hashes here.

Provenance

The following attestation bundles were made for orion_cache-0.1.3-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