agentleFS
28672032962867203296/hermes-agent/skills/software-development/caching-strategies/SKILL.md

caching-strategies

Skill0 starsChanged 3 months agoMITView on GitHub

缓存全攻略:Redis模式、失效策略、CDN、浏览器缓存.

11 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use observability and api-design.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • OpenAISkill

    observability

    Vercel Observability expert guidance — Drains (logs, traces, speed insights, web analytics), Web Analytics, Speed Insights, runtime logs, custom events, OpenTelemetry integration, and monitoring dashboards. Use when instrumenting, debugging, or optimizing application performance and user experience on Vercel.

    Also in 3 projects that use this

  • MicrosoftSkill

    api-design

    Guide for designing and documenting RESTful APIs. Use when asked to design an API, create endpoints, or document an API.

    Also in 3 projects that use this

The file

Files
SKILL.md

缓存全攻略

缓存金字塔

浏览器缓存 (Cache-Control/ETag)
  → CDN (CloudFlare)
    → 反向代理 (Nginx)
      → 应用缓存 (Redis/Memcached)
        → 数据库缓存 (Buffer Pool)
          → 数据库

越上层越快,但容量越小。

Redis 常用模式

# 1. Cache-Aside(最常用)
def get_user(user_id):
    cached = redis.get(f"user:{user_id}")
    if cached:
        return json.loads(cached)
    user = db.query(User).get(user_id)
    redis.setex(f"user:{user_id}", 3600, json.dumps(user))
    return user

# 2. 计数器
redis.incr("page_views")              # 页面访问
redis.incrby("likes:post:42", 1)      # 点赞

# 3. 分布式锁
lock = redis.set("lock:task", "1", nx=True, ex=10)
if lock:
    try:
        do_work()
    finally:
        redis.delete("lock:task")

# 4. 消息队列
redis.lpush("queue", task_json)       # 生产者
task = redis.brpop("queue", timeout=5) # 消费者(阻塞等)

# 5. 排行榜(Sorted Set)
redis.zadd("scores", {"user1": 100, "user2": 95})
redis.zrevrange("scores", 0, 9, withscores=True)  # Top10

三大缓存问题

缓存穿透:查不存在的数据 → 每次都穿到DB
  → 方案:布隆过滤器 + 缓存空值

缓存击穿:热点数据过期 → 大量请求同时打DB
  → 方案:互斥锁(只有一个线程去查DB重建缓存)

缓存雪崩:大量缓存同时过期 → DB瞬间被打爆
  → 方案:过期时间加随机值(3600±600秒)

缓存失效策略

TTL(生存时间):设过期时间,到期自动删除
LRU(最近最少用):内存满时淘汰最久没用的
LFU(最不常用):内存满时淘汰使用频率最低的

Redis 默认:volatile-lru(有过期时间的键用LRU)

浏览器缓存

# 强缓存(不发请求)
Cache-Control: max-age=3600        # 缓存1小时
Cache-Control: immutable           # 永远不变(适合带hash的文件)

# 协商缓存(发请求验证)
ETag: "abc123"                     # 内容指纹
Last-Modified: Wed, 30 Jul 2026    # 最后修改时间
If-None-Match: "abc123"            # 请求时带上,304表示没变

CDN

原理:把静态资源推到离用户最近的节点。

适合:图片、CSS/JS、视频、下载文件
不适合:动态内容、用户相关数据

回源:CDN节点没有 → 回源站取 → 缓存到CDN
刷新:主动清除CDN缓存
预热:提前把资源推到CDN节点

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.