Skip to main content

Muffin-Redis

Muffin-Redis — Redis support for the Muffin framework.

Tests Status PyPI Version Python Versions


Features

  • Built on redis.asyncio client.
  • Optional support for fakeredis for testing without a real Redis server.
  • JSON serialization support
  • Automatic connection pool management (blocking or non-blocking).
  • Simple get() / set() with optional jsonify flag.
  • Async locks with automatic release.
  • Lifecycle support: startup and shutdown integrate with Muffin app lifecycle.

Requirements

  • Python >= 3.11 (tested on 3.11 - 3.15)

Installation

Install using pip:

pip install muffin-redis

Optionally with fakeredis:

pip install muffin-redis[fake]

Usage

Setup the plugin and connect it into your app:

from muffin import Application
from muffin_redis import Plugin as Redis

app = Application('example')

# Initialize the plugin
redis = Redis(url='redis://localhost')
redis.setup(app)

Only the options from the table below are read from the configuration, anything else is ignored (so a mistyped option name silently falls back to its default). Everything else is passed to redis.asyncio.Redis, so the redis-py documentation applies to any command or option not listed here.

You can now use Redis in your routes:

@app.route('/some_url', methods=['POST'])
async def some_method(request):
    value = await redis.get('key', jsonify=True)
    if value is None:
        value = {'some': 'value'}  # Do some work
        await redis.set('key', value, jsonify=True, ex=60)
    return value

Configuration Options

Name Default Description
url "redis://localhost" Redis connection URL
db None Redis DB index (None keeps the server default, 0)
password None Redis password
encoding "utf-8" Encoding used by the client
poolsize 10 Max pool size (0 means no limit and lets redis-py use its default of 100)
decode_responses True Whether to decode binary responses
jsonify False Automatically encode/decode JSON with get/set
blocking True Use a blocking connection pool
timeout 20 Seconds to wait for a free connection (blocking pool only)
fake False Use fakeredis instead of a real Redis server

A db in the URL path (redis://localhost/2) wins over the db option. With fake: true the url, password and poolsize options are ignored (the server is local); db is kept.

API

client

Property returning the underlying redis.asyncio.Redis instance. Raises RuntimeError when the plugin is not started yet.

set(name, value, *, jsonify=None, **options)

Coroutine storing value under name and returning the client's result (True on success). jsonify falls back to the jsonify config option. **options are passed to the Redis SET command, e.g. ex=60 (expire in seconds), px=..., nx=True, xx=True. Note that the keyword is ex, not expire.

get(key, *, jsonify=None)

Coroutine returning the stored value, or None when the key does not exist. With jsonify enabled the value (a str because decode_responses defaults to True) is parsed as JSON; values that are not valid JSON are returned unchanged.

lock(name, *, timeout=30, blocking=True, blocking_timeout=None, sleep=0.1)

Async context manager yielding True when the lock was acquired and False otherwise. The lock is released on a normal or exceptional exit, and only if it is still held by us (an expired lock is left alone).

  • timeout — lock lifetime in seconds; the lock expires automatically, None disables expiry.
  • blocking — wait for a busy lock instead of returning False immediately.
  • blocking_timeout — how long a blocking acquire waits before giving up (None waits until the current holder's timeout expires).
  • sleep — delay between acquire attempts.
async with redis.lock('reports', timeout=10) as acquired:
    if acquired:
        ...  # only one worker at a time

With fake=True the lock is emulated with SET NX/DEL polling, so it has no Lua dependency and no distributed guarantees.

Everything else

Unknown attributes are proxied to the client, so regular Redis commands work directly:

await redis.hgetall('some-hash')
await redis.publish('chan:1', 'test')

Inside an application the plugin is started and stopped with the app lifecycle; standalone it can be used as an async context manager (async with redis: ...).

Bug Tracker

Please report bugs or feature requests at: https://github.com/klen/muffin-redis/issues

Contributing

Development happens at: https://github.com/klen/muffin-redis

License

Licensed under the MIT license.

Credits

  • Created by klen (Kirill Klenov)

Metadata

Release files for muffin-redis 4.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for muffin-redis 4.1.0
File Size Uploaded Provenance Actions
muffin_redis-4.1.0.tar.gz 6.6 kB Without provenance Details for muffin_redis-4.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for muffin-redis 4.1.0
File Interpreter ABI Platform Actions
muffin_redis-4.1.0-py3-none-any.whl Python 3 none any Details for muffin_redis-4.1.0-py3-none-any.whl

Total release size: 13.4 kB

Release files / muffin_redis-4.1.0.tar.gz

Download URL muffin_redis-4.1.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b9ecec37d3e077ce5af793f7224e52b398d48e766f6d9e23920e2487df36be17
BLAKE2b-256 checksum
How to use checksums
20ac3418b68a5ef9499a5bed6d52daabf47be6aa3cb0b63b72b93331ea9d821b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / muffin_redis-4.1.0-py3-none-any.whl

Download URL muffin_redis-4.1.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e59c40c28ed62ccb30d5f6b15f77af1d74a408d217d3b95d67ab671b620aee6
BLAKE2b-256 checksum
How to use checksums
c24c9dd33221c6af652b3bdf5ed8a541267ea6336bde0fd428fc3c4748b3e786
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for muffin-redis 4.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page