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.3.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.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: redis_memory-0.3.3.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.3.tar.gz
Algorithm Hash digest
SHA256 4cc4bcb2e79154acf7a52d8beb38b2511eaf4eea9e0a62ce57418700109c489f
MD5 9c6ab65f51722a1e04d9af455997d2f5
BLAKE2b-256 74c23ace8b7277e335d784b6ffd1d3c3a87f7d2bfe8a2845bba50b010f812b9e

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: redis_memory-0.3.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17acfc889ec52266db41f7f7f8c8ae38112492e2ff0e724a2a7ed1cf0b89f2fd
MD5 9299ea128fe32f03c90a0037378e281a
BLAKE2b-256 53f4f5c540292ee6dab8ba3fc5c443f1cad2a6c45b1d957c1a8cdec28ad7d14d

See more details on using hashes here.

Provenance

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