Skip to main content

An object that persists in Redis. Works across instances and works seamlessly through magic functions.

Project description

Tests & Lint PyPI PyPI downloads License

🗄️ 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.3.2.tar.gz (13.0 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.3.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: redis_memory-0.3.2.tar.gz
  • Upload date:
  • Size: 13.0 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.3.2.tar.gz
Algorithm Hash digest
SHA256 af9a39369a93deec6289432cbf630a26008fbba49fdac1609e6d48c0dbb748df
MD5 c031af83d28f5aff7a66fb7722f8952c
BLAKE2b-256 e9ae2c9ab5a73c006e52f98380cc13ef8be3598c8cad3106b2195349e784d641

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_memory-0.3.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.3.2-py3-none-any.whl.

File metadata

  • Download URL: redis_memory-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 36870a888e2c4e9c12d15546a1d549f5611591c92ee6fccac8c46e4db2b6f828
MD5 88f5c7dbbae31571af3c8a50619426bc
BLAKE2b-256 5232ed110e6b8aa43402a82f1e7d1f5a458bf0f66c3dc05840a82439139e46fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_memory-0.3.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