A lightweight and efficient Redis storage manager for Python applications
Simple CRUD operations with JSON serialization and hash-based data organization.
smart-redis-storage — a lightweight and efficient Redis storage manager for Python applications. Provides simple CRUD (Create, Read, Update, Delete) operations with JSON serialization and hash-based data organization.
Requirements: Python 3.6+, Redis server, redis-py library.
pip install smart-redis-storage
Requirements: Python 3.6+, Redis server, redis-py library
from smart_redis_storage.redis_storage import RedisStorageManager
# Initialize with default Redis connection (localhost:6379)
redis_storage = RedisStorageManager()
# Store user data
user_id = 123
redis_storage.set_data(
uniq_key=user_id,
key='profile',
value={'name': 'John', 'age': 30, 'email': 'john@example.com'}
)
# Retrieve user data
profile = redis_storage.get_data(uniq_key=user_id, key='profile')
print(profile) # {'name': 'John', 'age': 30, 'email': 'john@example.com'}
# Store with expiration (30 seconds)
redis_storage.set_data(
uniq_key=user_id,
key='session_data',
value={'token': 'abc123', 'last_login': '2026-01-01'},
expiration=30
)
Custom Redis Connection:
redis_storage = RedisStorageManager(
host='redis-server.com',
port=6379,
db=1
)
Multiple Data Operations:
user_id = 456
redis_storage.set_data(user_id, 'preferences', {'theme': 'dark', 'language': 'en'})
redis_storage.set_data(user_id, 'cart', {'items': [1, 2, 3], 'total': 150.0})
# Get all data for a user
all_user_data = redis_storage.get_all_data(user_id)
# Update specific data
redis_storage.update_data(user_id, 'preferences', {'theme': 'light', 'language': 'en'})
# Pop data (read and remove)
cart_data = redis_storage.pop_data(user_id, 'cart')
# Check remaining TTL
ttl = redis_storage.get_ttl(user_id)
Data Management:
# Delete specific key redis_storage.delete_key(user_id, 'preferences') # Delete all data for a unique key redis_storage.delete_all_hash(user_id)
Session Management:
def store_user_session(user_id, session_data):
redis_storage.set_data(
uniq_key=user_id,
key='session',
value=session_data,
expiration=3600 # 1 hour
)
Cache Management:
def get_cached_data(user_id, data_key, compute_function):
cached = redis_storage.get_data(user_id, data_key)
if cached is not None:
return cached
result = compute_function()
redis_storage.set_data(user_id, data_key, result, expiration=300)
return result
Multi-tenant Data Isolation:
tenant_a_data = redis_storage.get_all_data('tenant_a')
tenant_b_data = redis_storage.get_all_data('tenant_b')
The library uses Redis hashes with the following pattern:
uniq:{unique_identifier}
uniq:123 (Hash)
├── profile → '{"name": "John", "age": 30}'
├── session → '{"token": "abc123", "last_login": "2026-01-01"}'
└── preferences → '{"theme": "dark", "language": "en"}'
RedisStorageManager — Main Class
| Method | Description |
|---|---|
set_data(uniq_key, key, value, expiration=None) | Store data with optional expiration |
get_data(uniq_key, key, pop=False) | Retrieve data, pop=True removes it |
get_all_data(uniq_key) | Retrieve all key-value pairs |
update_data(uniq_key, key, value, expiration=None) | Update existing data |
pop_data(uniq_key, key) | Retrieve and remove data |
delete_key(uniq_key, key) | Remove specific key |
delete_all_hash(uniq_key) | Remove all data for identifier |
get_ttl(uniq_key) | Get remaining time-to-live |
Constructor: RedisStorageManager(host='localhost', port=6379, db=0)
try:
redis_storage.set_data(user_id, 'key', {'data': 'value'})
except redis.ConnectionError as e:
print(f"Redis connection error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
By using this software, you agree to the full disclaimer terms.
Software provided "AS IS" without warranty. You assume all risks.
Full legal disclaimer: See DISCLAIMER.md
License: BSD 3-Clause License