关于 redis
面向 Redis 开发者的数据结构与命令参考,涵盖常见操作、事务和消息传递,并指向缓存及实时应用的 Redis 模式学习内容。
主要功能
- 01字符串操作与过期设置
- 02列表、哈希、集合及有序集合命令
- 03键管理与过期设置
- 04事务与原子执行
- 05Pub/Sub 消息及服务器命令
适用场景
- 01开发应用时查询 Redis 命令
- 02参考原子事务操作方式
- 03使用 Pub/Sub 进行频道消息传递
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-sql --skill redis安装后在 Claude Code 中输入/redis确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
patterns.mdassets/config.yamlreferences/GUIDE.mdscripts/helper.py详细介绍
Redis 数据结构
入门
# Start Redis server
redis-server
# Connect to Redis CLI
redis-cli
# Test connection
ping # Returns "PONG"
# Select database
SELECT 0 # Default database
SELECT 1 # Database 1
字符串操作
// SET and GET
SET key value
SET user:1:name "John Doe"
GET user:1:name
// SET with options
SET key value EX 3600 // Expire in 3600 seconds
SET key value PX 3600000 // Expire in milliseconds
SET key value NX // Only if not exists
SET key value XX // Only if exists
// Numeric operations
SET counter 0
INCR counter // Increment by 1
INCRBY counter 5 // Increment by N
DECR counter // Decrement by 1
DECRBY counter 3 // Decrement by N
INCRBYFLOAT counter 2.5 // Increment by float
// String operations
APPEND key " suffix" // Append to string
STRLEN key // Get length
GETRANGE key 0 3 // Get substring
SETRANGE key 0 "new" // Set substring
// Multiple keys
MSET key1 val1 key2 val2 // Set multiple
MGET key1 key2 // Get multiple
GETSET key newval // Get old value and set new
列表操作(有序集合)
// Push operations
LPUSH list value1 value2 // Push to left
RPUSH list value1 value2 // Push to right
LPUSHX list value // Push only if exists
RPUSHX list value // Push only if exists
// Pop operations
LPOP list // Remove and get from left
RPOP list // Remove and get from right
LPOP list 2 // Pop multiple (Redis 6.2+)
// List queries
LRANGE list 0 -1 // Get all elements
LRANGE list 0 2 // Get first 3 elements
LINDEX list 1 // Get element at index
LLEN list // Get list length
LSET list 0 newvalue // Set element at index
// Blocking operations
BLPOP list1 list2 10 // Block until pop or timeout
BRPOP list1 list2 10 // Block until right pop
BRPOPLPUSH src dst 10 // Block, pop right, push left
// Trimming
LTRIM list 0 2 // Keep only first 3 elements
哈希操作(映射/对象)
// SET and GET
HSET hash field value // Set single field
HSET hash f1 v1 f2 v2 // Set multiple fields
HGET hash field // Get field value
HGETALL hash // Get all fields and values
// Existence and length
HEXISTS hash field // Check field exists
HLEN hash // Number of fields
HKEYS hash // Get all field names
HVALS hash // Get all values
HSTRLEN hash field // Get value length
// Update operations
HINCRBY hash field 5 // Increment numeric field
HINCRBYFLOAT hash field 2.5 // Increment by float
HSETNX hash field value // Set only if not exists
// Delete
HDEL hash field1 field2 // Delete fields
集合操作(无序唯一值)
// Add and remove
SADD set member1 member2 // Add members
SREM set member1 member2 // Remove members
SISMEMBER set member // Check membership
SMEMBERS set // Get all members
SCARD set // Count members
// Set operations
SINTER set1 set2 // Intersection
SUNION set1 set2 // Union
SDIFF set1 set2 // Difference
SINTERSTORE dest s1 s2 // Store intersection result
SUNIONSTORE dest s1 s2 // Store union result
SDIFFSTORE dest s1 s2 // Store difference result
// Pop operations
SPOP set // Remove and return random member
SPOP set 2 // Remove and return N members
SRANDMEMBER set // Get random member without removing
SRANDMEMBER set 3 // Get N random members
有序集合操作(按分数排序)
// Add and remove
ZADD zset 1 member1 2 member2 // Add with scores
ZREM zset member1 // Remove members
ZCARD zset // Count members
ZSCORE zset member // Get score
// Range queries by score
ZRANGE zset 0 -1 // Get all by index
ZRANGE zset 0 -1 WITHSCORES // With scores
ZREVRANGE zset 0 -1 // Reverse order
ZREVRANGE zset 0 -1 WITHSCORES // Reverse with scores
ZRANGEBYSCORE zset 10 50 // Get by score range
ZRANGEBYSCORE zset -inf +inf // All scores
ZRANGEBYSCORE zset 10 50 LIMIT 0 5 // Pagination
// Score operations
ZINCRBY zset 5 member // Increment score
ZCOUNT zset 10 50 // Count in score range
// Rank queries
ZRANK zset member // Get rank (0-based)
ZREVRANK zset member // Get reverse rank
键操作
// Key management
KEYS pattern // Find keys matching pattern
EXISTS key1 key2 // Check key existence
DEL key1 key2 // Delete keys
UNLINK key1 key2 // Async delete
TYPE key // Get key type
// Expiration
EXPIRE key 3600 // Set expiration (seconds)
PEXPIRE key 3600000 // Set expiration (milliseconds)
TTL key // Get TTL (seconds)
PTTL key // Get TTL (milliseconds)
PERSIST key // Remove expiration
// Renaming
RENAME oldkey newkey // Rename key
RENAMENX oldkey newkey // Rename only if new doesn't exist
事务与原子性
// Transaction execution
MULTI // Start transaction
SET key1 value1
INCR key2
GET key3
EXEC // Execute all commands atomically
// Discard transaction
MULTI
SET key value
DISCARD // Cancel transaction
// Watch keys
WATCH key1 key2 // Monitor keys for changes
MULTI
SET key1 newvalue
EXEC // Fails if keys changed
发布/订阅消息
// Publisher
PUBLISH channel "message" // Publish to channel
// Subscriber
SUBSCRIBE channel1 channel2 // Subscribe to channels
PSUBSCRIBE pattern* // Subscribe to pattern
UNSUBSCRIBE channel // Unsubscribe
PUNSUBSCRIBE pattern // Unsubscribe from pattern
// Query subscriptions
PUBSUB CHANNELS // Active channels
PUBSUB NUMSUB ch1 ch2 // Subscribers per channel
PUBSUB NUMPAT // Pattern subscriptions count
服务端命令
DBSIZE // Total keys in DB
FLUSHDB // Clear current DB
FLUSHALL // Clear all DBs
SAVE // Synchronous save
BGSAVE // Background save
LASTSAVE // Last save time
INFO // Server statistics
CONFIG GET parameter // Get config value
CONFIG SET parameter value // Set config value
后续步骤
在 redis-patterns 技能中了解 Redis 在缓存、会话、速率限制和实时应用中的模式。
评论
支持 Markdown