Integration
Redis Integration Application Integration
python
# Python Redis integration
import redis
# Connect to Redis
r = redis.Redis(
host='localhost',
port=6379,
password='your_password',
decode_responses=True
)
# Basic operations
r.set('key', 'value')
value = r.get('key')
# Hash operations
r.hset('user:1', 'name', 'John')
r.hset('user:1', 'email', 'john@example.com')
user_data = r.hgetall('user:1')
# List operations
r.lpush('tasks', 'task1')
r.lpush('tasks', 'task2')
tasks = r.lrange('tasks', 0, -1)
# Set operations
r.sadd('tags', 'redis', 'database', 'cache')
tags = r.smembers('tags')
# Sorted set operations
r.zadd('leaderboard', {'player1': 100, 'player2': 200})
top_players = r.zrange('leaderboard', 0, -1, withscores=True)
Connection Pooling
python
# Redis connection pooling
import redis
pool = redis.ConnectionPool(
host='localhost',
port=6379,
password='your_password',
max_connections=20,
decode_responses=True
)
r = redis.Redis(connection_pool=pool)
# Use connection from pool
r.set('pooled_key', 'pooled_value')
Cluster Integration
python
# Redis Cluster integration
from rediscluster import RedisCluster
startup_nodes = [
{"host": "127.0.0.1", "port": "7000"},
{"host": "127.0.0.1", "port": "7001"},
{"host": "127.0.0.1", "port": "7002"}
]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
rc.set('cluster_key', 'cluster_value')
Pub/Sub Integration
python
# Redis Pub/Sub
import redis
r = redis.Redis(host='localhost', port=6379)
pubsub = r.pubsub()
# Subscribe to channel
pubsub.subscribe('notifications')
# Publish message
r.publish('notifications', 'Hello World')
# Listen for messages
for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received: {message['data']}")