Skip to main content

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.json for 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redis_memory-0.2.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

redis_memory-0.2.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file redis_memory-0.2.2.tar.gz.

File metadata

  • Download URL: redis_memory-0.2.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for redis_memory-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e74544a01cda1d3a9c8e1d77df9c38e374e981eb58bafeeead73d1ef129ebe75
MD5 c12e3517f7c5293aae5c5b0dfaf5db2c
BLAKE2b-256 85823695a8065a9daca0b397e661403a82d5d8d0e096bd4f6a02b741df72b502

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_memory-0.2.2.tar.gz:

Publisher: ci.yaml on sinan-ozel/redis-memory

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

File details

Details for the file redis_memory-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: redis_memory-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for redis_memory-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bc52e8dda331caceb25a26326d98266a0fe93fd108adf4fe076b17405d8aec6
MD5 10a0263efece079302c4a5fa42c322e6
BLAKE2b-256 9d20e520bf751f3ee78073f163531db10799ce088f46311a55156a5e4ff48a04

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_memory-0.2.2-py3-none-any.whl:

Publisher: ci.yaml on sinan-ozel/redis-memory

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