Announcement

👇Official Account👇

Welcome to join the group & private message

Article first/tail QR code

Skip to content

Lesson 3.3: Redis 缓存设计

学习目标

  • 掌握 Redis 核心数据结构和应用场景
  • 设计高效的缓存策略

1. 数据结构与应用

类型底层实现典型场景
StringSDS缓存、计数器、分布式锁
Hashdict + ziplist对象存储(用户信息)
Listquicklist消息队列、最新消息
Setintset + dict标签、去重、交集/并集
ZSetziplist + skiplist排行榜、延迟队列

2. 缓存策略

go
// Cache-Aside 模式(最常用)
func GetUser(ctx context.Context, id string) (*User, error) {
    // 1. 查缓存
    val, err := redis.Get(ctx, "user:"+id).Result()
    if err == nil {
        user := &User{}
        json.Unmarshal([]byte(val), user)
        return user, nil
    }

    // 2. 未命中,查数据库
    user, err := db.GetUser(id)
    if err != nil {
        return nil, err
    }

    // 3. 写入缓存(设置过期时间)
    data, _ := json.Marshal(user)
    redis.Set(ctx, "user:"+id, data, 30*time.Minute)

    return user, nil
}

3. 常见问题

问题原因解决方案
缓存穿透查不存在的数据布隆过滤器 / 缓存空值
缓存雪崩大量 key 同时过期过期时间加随机值
缓存击穿热点 key 过期互斥锁 / 永不过期

推荐阅读

上次更新于: