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.
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 viadefault=str
Requirements
- Python 3.9+
- Redis 5+
Contributing
Contributions are welcome!
- Fork the repo and clone it locally
- Create a virtual environment and install in editable mode:
python -m venv .venv source .venv/bin/activate pip install -e ".[dev]"
- Make your changes
- Run tests before submitting:
pytest
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e1bde9f77156ee1a47d1020bdbd8c54406bb53edd72c1b5f1b3b0518f32cd3d
|
|
| MD5 |
617eb3ae56e731d21b639ad19c2592ff
|
|
| BLAKE2b-256 |
fcd730b74f920a36d776cd2e28060476c75f2cfc34f46408a0cd8074f3875696
|
Provenance
The following attestation bundles were made for orion_cache-0.1.3.tar.gz:
Publisher:
python-publish.yml on aliinreallife/orion-cache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
orion_cache-0.1.3.tar.gz -
Subject digest:
7e1bde9f77156ee1a47d1020bdbd8c54406bb53edd72c1b5f1b3b0518f32cd3d - Sigstore transparency entry: 962550892
- Sigstore integration time:
-
Permalink:
aliinreallife/orion-cache@17cd04e3f7242decd41bdbedbdb0b3f231b74ad1 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/aliinreallife
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@17cd04e3f7242decd41bdbedbdb0b3f231b74ad1 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00603c0daed62ec83fc2e121ba9022092441e3db7ff342875cd17f4b7f24cd0a
|
|
| MD5 |
007e72757f483c975f714887ddaba10a
|
|
| BLAKE2b-256 |
310790937091090dc1c0d645b8d643322246f31eae1bf76cd6095a9db1594c25
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
orion_cache-0.1.3-py3-none-any.whl -
Subject digest:
00603c0daed62ec83fc2e121ba9022092441e3db7ff342875cd17f4b7f24cd0a - Sigstore transparency entry: 962550898
- Sigstore integration time:
-
Permalink:
aliinreallife/orion-cache@17cd04e3f7242decd41bdbedbdb0b3f231b74ad1 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/aliinreallife
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@17cd04e3f7242decd41bdbedbdb0b3f231b74ad1 -
Trigger Event:
push
-
Statement type: