跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
效率与协作

Caching Strategies

Redis caching patterns including Cache-Aside, Write-Through, and invalidation.

j4flmao · 已有静态检查记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add j4flmao/agent-skills --skill Caching Strategies

技能说明


name: Caching Strategies description: Redis caching patterns including Cache-Aside, Write-Through, and invalidation.

Caching Strategies with Redis

Patterns

  • Cache-Aside: Application checks cache first, if miss, loads from DB and updates cache.
  • Write-Through: Application writes data to both cache and DB simultaneously.

Invalidation

Ensure stale data is removed via TTLs or explicit deletion on updates.

Diagram

%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
flowchart TD
    A[App] -->|1. Read| B{Cache}
    B -- Hit --> A
    B -- Miss --> C[(Database)]
    C -->|2. Return Data| A
    A -->|3. Update Cache| B

Go Example (Cache-Aside)

package main

import (
    "context"
    "github.com/go-redis/redis/v8"
    "time"
)

var ctx = context.Background()

func getUser(rdb *redis.Client, db Database, userID string) (string, error) {
    val, err := rdb.Get(ctx, userID).Result()
    if err == nil {
        return val, nil // Cache hit
    }

    // Cache miss, fetch from DB
    user, err := db.GetUser(userID)
    if err != nil {
        return "", err
    }

    // Update cache
    rdb.Set(ctx, userID, user, 10*time.Minute)
    return user, nil
}