Skip to main content

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

Project description

Tests & Lint PyPI Downloads Monthly Downloads License Documentation

🗄️ redis-memory

A production-ready 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.

Purpose

The intention is to use this with agentic workflows deployed as microservices, allowing for multiple instances of the same pod to share their state.

✨ 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"

� Auto-Synced Collections

Lists and dictionaries are automatically wrapped as SyncedList and SyncedDict, which sync changes to Redis immediately:

mem = Memory()
mem.items = [1, 2, 3]
mem.items.append(4)  # Automatically syncs to Redis

mem2 = Memory()
print(mem2.items)  # [1, 2, 3, 4]

mem.config = {"theme": "dark"}
mem.config["lang"] = "en"  # Automatically syncs to Redis
print(mem2.config)  # {'theme': 'dark', 'lang': 'en'}

Nested structures work too:

mem.data = {"user": {"preferences": {"color": "blue"}}}
mem.data["user"]["preferences"]["color"] = "red"  # Syncs!

Converting to Plain Python Types

For libraries that need plain Python objects (serialization, pickling, etc.):

mem.items = [1, 2, 3]
plain_list = mem.items.aslist()  # Returns regular list

mem.config = {"key": "value"}
plain_dict = mem.config.asdict()  # Returns regular dict

# Now you can pickle, deepcopy, or pass to external libraries
import pickle
pickle.dump(plain_list, file)  # Works!

🗂️ Namespacing

By default, redis-memory uses memory: as its Redis prefix.

Set that with REDIS_PREFIX:

REDIS_PREFIX="my_service_state:"
from redis_memory import Memory

mem = Memory()
mem.state = {"step": 1}
print(mem.state)  # {'step': 1}

Agents

Use the specific class ConversationMemory as an agent memory:

# TODO: Expand with a real conversation object and/or LiteLLM

from redis_memory import ConversationMemory

conversation_id = uuid()

mem = ConversationMemory(conversation_id=conversation_id)
mem.messages = messages  # The messages payload to an LLM.

⚙️ 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 a Development Container: Open in VS Code, and start the development container. You do not need to install anything other than VS Code and docker. (Shift/Cmd + P and check under ``Dev Containers'')

Run the two VS code tasks, test and reformat, before making a PR. These are the same as the tests that will run on the CI/CD pipeline.

Without Anything: Just write the code, and add your unit tests. (Test-Driven Development) Run the following command:

docker-compose up --build --remove-orphans --force-recreate --abort-on-container-exit --exit-code-from test

The only requirement is docker.

Virtual Env: I did not add support for the Python venv. However, all of the requirements are captured in pyproject.toml. You should be able to use the following commands to set up a venv with all of the requirements.

python -m venv .venv
source .venv/bin/activate       # macOS/Linux
.venv\Scripts\activate          # Windows

pip install --upgrade pip
pip install .
pip install .[test]
pip install .[dev]

🤝 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.4.tar.gz (15.9 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.4-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for redis_memory-0.3.4.tar.gz
Algorithm Hash digest
SHA256 4c6af19be815999e28cde7438ea1834911ec3effd4c3205d71b5c584244b9932
MD5 c2b1b8856cf0235be4c2a8b2400e621b
BLAKE2b-256 b01bf4575a96774f0d079e4bcd74fee0b8faa6ec6230625ed1bb193b079d6fb1

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for redis_memory-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a68c56c2dc43f7488b2572b04a8a0cb34389190a9912001c32691faeb6a55dea
MD5 cdc9d33270778994eeb3172dbdd2098c
BLAKE2b-256 1f4e350c8b55cba4576a37960b20172deba067ff7004e8b126e06fb9a4d5fd3f

See more details on using hashes here.

Provenance

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