Comprehensive assistance with Redis development, generated from official documentation.
When to Use This Skill
This skill should be triggered when:
Implementing caching layers or session storage
Building real-time applications (pub/sub, leaderboards, analytics)
Working with Redis data types (strings, hashes, lists, sets, sorted sets)
Setting up Redis persistence (RDB, AOF)
Implementing distributed locks or rate limiting
Using Redis for vector search and AI/ML applications
Configuring Redis clusters or enterprise deployments
Debugging Redis performance or memory issues
Integrating Redis with Python, Node.js, or other languages
Quick Reference
Basic Key-Value Operations
관련 스킬
# Set a string value
SET user:1000 "John Doe"
# Set with expiration (10 seconds)
SET session:xyz "token123" EX 10
# Get a value
GET user:1000
# Delete keys
DEL user:1000 session:xyz
# Check time to live
TTL session:xyz
Working with Hashes
# Set multiple hash fields
HSET user:1001 name "Jane Smith" email "[email protected]" age 28
# Get a single field
HGET user:1001 name
# Get all fields and values
HGETALL user:1001
# Increment a numeric field
HINCRBY user:1001 age 1
Lists for Queues
import redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# Push items to a queue
r.lpush('jobs:queue', 'job1', 'job2', 'job3')
# Pop from queue (FIFO)
job = r.rpop('jobs:queue')
# Blocking pop (waits for items)
job = r.brpop('jobs:queue', timeout=5)
Sets for Unique Collections
# Add members to a set
SADD tags:article:1 "redis" "database" "cache"
# Check membership
SISMEMBER tags:article:1 "redis"
# Get all members
SMEMBERS tags:article:1
# Set operations
SINTER tags:article:1 tags:article:2 # Intersection
SUNION tags:article:1 tags:article:2 # Union
Sorted Sets for Leaderboards
import redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
# Add scores
r.zadd('leaderboard', {'player1': 100, 'player2': 150, 'player3': 80})
# Get top 3 players (highest scores)
top_players = r.zrevrange('leaderboard', 0, 2, withscores=True)
# Get rank of a player
rank = r.zrevrank('leaderboard', 'player1')
# Increment score
r.zincrby('leaderboard', 10, 'player1')
Expiration and TTL
# Set expiration on existing key
EXPIRE user:session:abc 3600
# Set key with expiration
SETEX cache:data 300 "cached value"
# Check remaining time
TTL cache:data
# Remove expiration
PERSIST cache:data
Pub/Sub Pattern
import redis
# Publisher
r = redis.Redis(host='localhost', port=6379)
r.publish('notifications', 'New message!')
# Subscriber
pubsub = r.pubsub()
pubsub.subscribe('notifications')
for message in pubsub.listen():
if message['type'] == 'message':
print(f"Received: {message['data']}")
Authentication
# Authenticate with password
AUTH mypassword
# Authenticate with username and password (ACL)
AUTH username mypassword
Distributed Locks
import redis
import time
r = redis.Redis(host='localhost', port=6379)
# Acquire lock
lock_key = 'resource:lock'
lock_value = 'unique-token-123'
# Try to acquire lock (NX = only if not exists)
acquired = r.set(lock_key, lock_value, nx=True, ex=10)
if acquired:
try:
# Perform critical section
print("Lock acquired, doing work...")
finally:
# Release lock only if we own it
if r.get(lock_key) == lock_value:
r.delete(lock_key)
Vector Search (AI/ML)
from redis import Redis
from redis.commands.search.field import VectorField, TextField
from redis.commands.search.indexDefinition import IndexDefinition
r = Redis(host='localhost', port=6379)
# Create index with vector field
schema = (
TextField("content"),
VectorField("embedding",
"FLAT",
{
"TYPE": "FLOAT32",
"DIM": 512,
"DISTANCE_METRIC": "COSINE"
}
)
)
r.ft("idx:vectors").create_index(
schema,
definition=IndexDefinition(prefix=["doc:"])
)
Reference Files
This skill includes comprehensive documentation in references/:
commands.md
Complete Redis commands reference including:
Core Commands: SET, GET, DEL, EXPIRE, TTL, AUTH
Hash Commands: HSET, HGET, HGETALL for working with hash data types
Data Structures: Lists, Sets, Sorted Sets operations