Redis client with msgpack serialization
Project description
redis-msgpack
A Python Redis client that uses MessagePack serialization for efficient data storage and retrieval.
Features
- Drop-in replacement for the standard Redis-py client
- Automatic MessagePack serialization and deserialization
- Significantly reduced memory usage and network transfer
- Full support for all Redis data types and commands
- Django cache backend integration
- Type hints for better IDE support
Installation
pip install redis-msgpack
With Django integration:
pip install redis-msgpack[django]
Basic Usage
from redis_msgpack import RedisMsgpackClient
# Connect to Redis
client = RedisMsgpackClient(host='localhost', port=6379, db=0)
# Store Python objects directly - they will be serialized with msgpack
client.set('user:profile', {
'username': 'johndoe',
'email': 'john@example.com',
'preferences': ['dark_mode', 'notifications_on'],
'login_count': 42,
'last_login': 1647853426.78
})
# Retrieve and automatically deserialize
user_profile = client.get('user:profile')
print(user_profile['username']) # 'johndoe'
Django Integration
Add to your Django settings:
CACHES = {
"default": {
"BACKEND": "redis_msgpack.django_integration.RedisMsgpackCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "redis_msgpack.client.RedisMsgpackClient",
"SERIALIZER_CLASS": "redis_msgpack.utils.MsgpackSerializer",
}
}
}
Then use Django's cache as usual:
from django.core.cache import cache
# Store complex data
cache.set('key', {'complex': 'data', 'numbers': [1, 2, 3]})
# Retrieve data
data = cache.get('key')
Advanced Usage
Pipeline Support
with client.pipeline() as pipe:
pipe.set('key1', 'value1')
pipe.set('key2', [1, 2, 3, 4])
pipe.hset('hash1', mapping={'field1': 'value1', 'field2': 'value2'})
result = pipe.execute()
PubSub Support
pubsub = client.pubsub()
pubsub.subscribe('channel1')
# Process messages
for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received: {message['data']}")
Custom Serialization Options
from redis_msgpack import RedisMsgpackClient
from redis_msgpack.utils import MsgpackSerializer
# Configure custom serialization options
serializer = MsgpackSerializer(use_bin_type=True, use_single_float=False)
client = RedisMsgpackClient(host='localhost', port=6379, serializer=serializer)
Performance Comparison
When compared to standard JSON serialization, redis-msgpack typically provides:
- 20-30% smaller serialized data size
- 10-20% faster serialization/deserialization
- Full support for binary data without additional encoding
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file redis_msgpack-0.1.0.tar.gz.
File metadata
- Download URL: redis_msgpack-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
704532891c467e1fd99e649de02a8f37d9d53ebbb0ec398744f3131b4698ca58
|
|
| MD5 |
082bf390e36f873d2d92aa1da78f9be0
|
|
| BLAKE2b-256 |
53813196f234896b7c36b4cac36231a0bcd9e6c2fdb03704a2ec94f7e096273f
|
File details
Details for the file redis_msgpack-0.1.0-py3-none-any.whl.
File metadata
- Download URL: redis_msgpack-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bafb1b352e5ced071d85063942beb3768e841c66a8f3ecff92be01041be72a12
|
|
| MD5 |
69f9612e4f3638899de21f6fa8a5723c
|
|
| BLAKE2b-256 |
4d906f107781800459dc9446f481ec34f357090a0e0b3185d8750e6ce4b5b01f
|