An object that persists in Redis. Works across instances and works seamlessly through magic functions.
Project description
🗄️ redis-memory
A Python class for seamless, multiprocessing-safe, persistent key-value storage using Redis as a backend. If Redis is unavailable, values are cached locally and queued for syncing when Redis comes back online. All values are serialized as JSON, and you interact with it using natural Python attribute access.
The intention is to use this with agentic workflows deployed as microservices, allowing for multiple instances of the same pod. (Hence the name ``memory'') That said, this is probably a good alternative for state management in microservice architecture where multiple pods are deployed in parallel.
✨ Features
- 🔄 Multiprocessing-safe: All processes share the same state via Redis.
- 🧠 Pythonic API: Set and get attributes as if they were regular object properties.
- 🕰️ Persistence: Values survive process restarts and context blocks.
- 🚦 Resilient: If Redis is down, changes are queued and flushed when it returns.
- 🧩 Customizable: Prefixes and conversation IDs for namespacing.
- 🧵 Background sync: Queued changes are flushed automatically in the background.
🚀 Quickstart
pip install redis-memory
from redis_memory import Memory
mem = Memory()
mem.answer = 42
print(mem.answer) # 42
# Across processes or instances:
mem2 = Memory()
print(mem2.answer) # 42
mem.settings = {"theme": "dark", "volume": 0.75}
print(mem.settings) # {'theme': 'dark', 'volume': 0.75}
🧑💻 Context Management
You can use Memory as a context manager for automatic resource handling:
with Memory() as memory:
memory.session = "active"
print(memory.session) # "active"
# Later, in a new context:
with Memory() as memory:
print(memory.session) # "active"
🗂️ Namespacing with ConversationMemory
For chatbots or multi-user apps, use ConversationMemory to isolate keys:
from redis_memory import ConversationMemory
conv_mem = ConversationMemory(conversation_id="user123")
conv_mem.state = {"step": 1}
print(conv_mem.state) # {'step': 1}
⚙️ Environment Variables
REDIS_HOST: Redis server hostname (default:redis)REDIS_PORT: Redis server port (default:6379)REDIS_PREFIX: Key prefix (default:memory:)
🛠️ Development
🐳 Docker/Devcontainer
- Clone the repo.
- You can use VS Code Dev Containers for an instant dev environment.
- Or, just run tests in Docker—no setup needed!
🧪 Running Tests
-
With Devcontainer: Open in VS Code, and use the built-in test tasks.
-
With Docker directly:
docker compose up -d redis docker run --rm -it -v $PWD:/workspace -w /workspace python:3.11 bash # Inside container: pip install -e . pytest
-
Or use the tasks in
.vscode/tasks.jsonfor one-click testing.
🤝 Contributing
- PRs are welcome! No special permissions required.
- All you need is Docker (or a devcontainer).
- Please ensure all tests pass before submitting your PR.
📚 License
MIT
Made with ❤️ and Redis.
Project details
Release history Release notifications | RSS feed
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 redis_memory-0.3.0.tar.gz.
File metadata
- Download URL: redis_memory-0.3.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81bdec2271149a825775b6be157b213888b701338cbd2047d29d81abb0f9bcd4
|
|
| MD5 |
eaf70349bb2d6d7b6342335b8ea804f3
|
|
| BLAKE2b-256 |
db9159fb3421d7ec1cdc80f2d75661b2f65cd8c90f91742f2cfc81f9606313d0
|
Provenance
The following attestation bundles were made for redis_memory-0.3.0.tar.gz:
Publisher:
ci.yaml on sinan-ozel/redis-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
redis_memory-0.3.0.tar.gz -
Subject digest:
81bdec2271149a825775b6be157b213888b701338cbd2047d29d81abb0f9bcd4 - Sigstore transparency entry: 458213250
- Sigstore integration time:
-
Permalink:
sinan-ozel/redis-memory@b81f30fd45de1d0f1eaa45a771f9b6e031351d62 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sinan-ozel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@b81f30fd45de1d0f1eaa45a771f9b6e031351d62 -
Trigger Event:
push
-
Statement type:
File details
Details for the file redis_memory-0.3.0-py3-none-any.whl.
File metadata
- Download URL: redis_memory-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5390b7bc463fb3730aeac6aca79e2245c7ee792dc09280a79f7bdc59e11c25fc
|
|
| MD5 |
2527966b43afff490e51e15e013ca52f
|
|
| BLAKE2b-256 |
5d688e7d22eec33d2a3e362003d5facc3cefff6040b239edf3930e589eb41d50
|
Provenance
The following attestation bundles were made for redis_memory-0.3.0-py3-none-any.whl:
Publisher:
ci.yaml on sinan-ozel/redis-memory
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
redis_memory-0.3.0-py3-none-any.whl -
Subject digest:
5390b7bc463fb3730aeac6aca79e2245c7ee792dc09280a79f7bdc59e11c25fc - Sigstore transparency entry: 458213252
- Sigstore integration time:
-
Permalink:
sinan-ozel/redis-memory@b81f30fd45de1d0f1eaa45a771f9b6e031351d62 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/sinan-ozel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@b81f30fd45de1d0f1eaa45a771f9b6e031351d62 -
Trigger Event:
push
-
Statement type: