Muffin-Redis — Redis support for the Muffin framework.
- Built on
redis.asyncioclient. - Optional support for
fakeredisfor testing without a real Redis server. - JSON serialization support
- Automatic connection pool management (blocking or non-blocking).
- Simple
get()/set()with optionaljsonifyflag. - Async locks with automatic release.
- Lifecycle support:
startupandshutdownintegrate with Muffin app lifecycle.
- Python >= 3.11 (tested on 3.11 - 3.15)
Install using pip:
pip install muffin-redisOptionally with fakeredis:
pip install muffin-redis[fake]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| 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.
Property returning the underlying redis.asyncio.Redis instance. Raises RuntimeError when the
plugin is not started yet.
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.
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.
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,Nonedisables expiry.blocking— wait for a busy lock instead of returningFalseimmediately.blocking_timeout— how long a blocking acquire waits before giving up (Nonewaits until the current holder'stimeoutexpires).sleep— delay between acquire attempts.
async with redis.lock('reports', timeout=10) as acquired:
if acquired:
... # only one worker at a timeWith fake=True the lock is emulated with SET NX/DEL polling, so it has no Lua dependency and
no distributed guarantees.
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: ...).
Please report bugs or feature requests at: https://github.com/klen/muffin-redis/issues
Development happens at: https://github.com/klen/muffin-redis
Licensed under the MIT license.
- Created by klen (Kirill Klenov)