Redis 核心数据结构
了解并应用字符串、哈希、列表、集合和有序集合,满足各种数据建模需求
Redis 核心数据结构 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Redis Data Types & Strings
Redis offers real data structures, not just plain keys. We start with Strings: a value up to 512 MB holding text, JSON, or binary — like a persistent variable.
Strings: Set & Get
Store and read a String with SET and GET. The code below sets a couple of keys and fetches them back.
SET mykey "Hello CoddyKit!"
GET mykey
SET user:1:name "Alice"
GET user:1:nameHashes: Store Objects
Redis Hashes are like objects: many field-value pairs under one key. Perfect for modeling things like a user profile with several attributes.
Hashes: HSET & HGETALL
Use HSET to set fields in a hash, HGETALL to read them all, and HGET for a single field. The code below builds and reads a user hash.
HSET user:1 name "Alice" email "alice@example.com" age 30
HGETALL user:1
HGET user:1 nameLists: Ordered Collections
Redis Lists are ordered string collections — push or pop from either end. Great for queues, stacks, and recent-activity feeds that keep insertion order.
Lists: LPUSH & LRANGE
LPUSH adds to the head, RPUSH to the tail, and LRANGE reads a range (0 -1 means everything). The code below pushes and lists items.
LPUSH mylist "task A" "task B"
RPUSH mylist "task C"
LRANGE mylist 0 -1
LPOP mylist
LRANGE mylist 0 -1Sets: Unique Items
Redis Sets are unordered collections of unique strings — duplicates are silently ignored. Ideal for unique visitors, tags, or friendships.
Sets: SADD & SMEMBERS
SADD adds members (dupes ignored), SMEMBERS lists them all, and SISMEMBER checks membership. The code below demonstrates all three.
SADD unique_users "user:101" "user:102" "user:101"
SMEMBERS unique_users
SISMEMBER unique_users "user:102"Sorted Sets: Ranked Data
Redis Sorted Sets pair each unique member with a score and keep them ordered by it — perfect for leaderboards, rankings, and priority queues.
Sorted Sets: ZADD & ZRANGE
ZADD adds scored members, ZRANGE reads a range (add WITHSCORES to see scores), and ZSCORE gets one member's score. See the code below.
ZADD leaderboard 100 "PlayerA" 150 "PlayerB" 75 "PlayerC"
ZRANGE leaderboard 0 -1 WITHSCORES
ZSCORE leaderboard "PlayerB"Data Structure Challenge
Imagine you need to store a list of unique product tags for an item, and you don't care about their order. Which Redis data structure is best suited for this?
Recap: Structures Mastered
Recap: Redis data structures — Strings for basics, Hashes for objects, Lists for ordered queues, Sets for unique items, Sorted Sets for ranked data.
常见问题解答
「Redis 核心数据结构」课时是免费的吗?
是的 — 「Redis 核心数据结构」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
「Redis 核心数据结构」这节课中我会学到什么?
了解并应用字符串、哈希、列表、集合和有序集合,满足各种数据建模需求 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「Redis 核心数据结构」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?
能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Redis 简介
- Redis CLI 基础
- Redis 核心数据结构
- Redis 中的密钥过期与 TTL