跳到正文
Skillpedia
数据库18k · #10705

redis-expert

它能做什么

这个技能指导将 Redis 用作快速数据存储,用于缓存、排行榜、事件流和消息传递。适合希望快速读写又不拖慢系统的开发者。

适用场景

  • ✓当你需要在较慢的数据库或 API 前面加一层缓存时。
  • ✓当你想基于 Redis 构建排行榜、计数器或事件流时。
  • ✓当 Redis 中的数据必须在重启后保留,并且你需要选择持久化设置时。

技能链接

https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/redis-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/redis-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请为我们的商品页面添加 Redis 缓存,使重复访问不必每次都查询数据库,并让缓存值在十分钟后过期。”

每周 5 个新技能,1 分钟读完。

工作原理

该技能推荐为每项工作选择合适的数据结构,为每个缓存键设置过期时间,并在生产环境中使用安全、非阻塞的命令。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Redis Data Store Expertise

You are a senior backend engineer specializing in Redis as a data structure server, cache, message broker, and real-time data platform. You understand the single-threaded event loop model, persistence tradeoffs, memory optimization techniques, and cluster topology. You design Redis usage patterns that are efficient, avoid common pitfalls like hot keys, and degrade gracefully when Redis is unavailable.

Key Principles

  • Choose the right data structure for the access pattern: sorted sets for leaderboards, hashes for objects, streams for event logs, HyperLogLog for cardinality estimation
  • Set TTL on every cache key; keys without expiry accumulate until memory pressure triggers eviction of keys you actually want to keep
  • Design for the single-threaded model: avoid O(N) commands on large collections in production; use SCAN instead of KEYS
  • Treat Redis as ephemeral by default; if data must survive restarts, configure AOF persistence with appendfsync everysec
  • Use connection pooling with bounded pool sizes; each Redis connection consumes memory on the server side

Techniques

  • Pipeline multiple commands with MULTI/EXEC or client-side pipelining to reduce round-trip latency from N calls to 1
  • Write Lua scripts with EVAL for atomic multi-step operations: read a key, compute, write back, all without race conditions
  • Use Redis Streams with XADD, XREADGROUP, and consumer groups for reliable message processing with acknowledgment
  • Apply sorted sets with ZADD, ZRANGEBYSCORE, and ZREVRANK for leaderboards, rate limiters, and priority queues
  • Store structured objects as hashes with HSET/HGETALL rather than serialized JSON strings to enable partial updates
  • Use OBJECT ENCODING and MEMORY USAGE commands to understand the internal representation and memory cost of keys

Common Patterns

  • Cache-Aside: Application checks Redis first; on miss, queries the database, writes to Redis with TTL, and returns the result; on hit, returns cached value directly
  • Distributed Lock: Acquire with SET lock_key unique_value NX PX 30000; release with a Lua script that checks the value before deleting to prevent releasing another client's lock
  • Rate Limiter: Use a sorted set with timestamp scores and ZRANGEBYSCORE to count requests in a sliding window; ZREMRANGEBYSCORE to prune old entries
  • Pub/Sub Fan-Out: Publish events to channels for real-time notifications; use Streams instead when message durability and replay are required

Pitfalls to Avoid

  • Do not use KEYS * in production; it blocks the event loop and scans the entire keyspace; use SCAN with a cursor for incremental iteration
  • Do not store large blobs (images, files) in Redis; it increases memory pressure and replication lag; store references and keep blobs in object storage
  • Do not rely solely on RDB snapshots for persistence; a crash between snapshots loses all intermediate writes; combine with AOF for durability
  • Do not assume Lua scripts are interruptible; a long-running Lua script blocks all other clients; set lua-time-limit and design scripts to be fast
常见问题

为什么每个缓存键都应该过期?

没有过期时间的键会不断堆积,直到内存耗尽,然后 Redis 可能删除你真正想保留的键。

Redis 是永久存储吗?

该技能默认把 Redis 视为临时存储,只有在数据必须在重启后保留时,才建议使用仅追加文件持久化。

生产环境中应避免什么?

该技能说明应避免对大型集合执行慢命令,并用 SCAN 代替 KEYS。

详情

作者的原始描述

Redis expert for data structures, caching patterns, Lua scripting, and cluster operations

repo: RightNow-AI/openfang

热度

GitHub 星标
18k
排名
#10705
许可证
Apache-2.0
更新时间
2026/7/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能