缓存策略与实现 logo

缓存策略与实现

Organization
microwind
缓存策略与实现

当设计和实现缓存系统时,分析缓存策略,优化缓存性能,解决缓存相关问题。验证缓存架构,设计缓存模式,和最佳实践。

Overview

Publishermicrowind
Repositoryai-skills
Skill name缓存策略与实现
Stars
93
Forks
25
Bundled files
3
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 缓存策略与实现 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/backend/caching-strategies
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/backend/caching-strategies .claude/skills/microwind-2
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 缓存策略与实现 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 缓存策略与实现 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 缓存策略与实现 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

缓存策略与实现技能

概述

缓存是提升系统性能的关键技术。不当的缓存策略会导致数据不一致、性能下降和系统复杂性增加。在设计缓存系统前需要仔细分析需求。

核心原则: 好的缓存策略应该显著提升性能,同时保持数据一致性。坏的缓存策略会增加系统复杂性,甚至降低性能。

何时使用

始终:

  • 设计新系统的缓存架构时
  • 优化现有系统性能时
  • 分析缓存命中率低的问题时
  • 设计分布式缓存方案时
  • 解决缓存穿透、雪崩、击穿问题时
  • 评估缓存策略的有效性时

触发短语:

  • "这个系统需要加缓存吗?"
  • "缓存命中率太低了"
  • "如何设计分布式缓存?"
  • "缓存穿透了怎么办?"
  • "Redis和Memcached怎么选?"
  • "热点数据怎么处理?"

缓存策略技能功能

缓存模式分析

  • Cache-Aside(旁路缓存)模式
  • Read-Through(穿透读)模式
  • Write-Through(穿透写)模式
  • Write-Behind(异步写)模式
  • Refresh-Ahead(预刷新)模式

缓存问题检测

  • 缓存穿透(大量请求不存在的数据)
  • 缓存雪崩(大量缓存同时失效)
  • 缓存击穿(热点数据失效)
  • 缓存污染(错误数据污染缓存)
  • 缓存一致性(数据同步问题)

性能优化检查

  • 缓存命中率分析
  • 缓存容量规划
  • 缓存过期策略
  • 缓存预热方案
  • 缓存监控指标

常见缓存设计问题

缓存穿透问题

问题:
大量请求查询不存在的数据,直接穿透到数据库

后果:
- 数据库压力剧增
- 系统响应变慢
- 可能导致数据库宕机

解决方案:
- 布隆过滤器拦截
- 缓存空值
- 参数校验前置
- 请求限流保护

缓存雪崩问题

问题:
大量缓存键在同一时间失效

后果:
- 数据库瞬时压力过大
- 系统响应超时
- 服务不可用

解决方案:
- 随机过期时间
- 缓存集群部署
- 熔断降级机制
- 多级缓存架构

缓存一致性

问题:
缓存数据与数据库数据不一致

后果:
- 用户看到错误数据
- 业务逻辑异常
- 数据完整性问题

解决方案:
- 设置合理的过期时间
- 使用消息队列同步
- 双写一致性策略
- 版本号机制

缓存策略选择指南

读多写少场景

推荐策略: Cache-Aside + 合理过期时间

适用场景:
- 商品信息查询
- 用户资料读取
- 配置信息获取

实现要点:
- 先查缓存,缓存没有再查数据库
- 查到数据库后写入缓存
- 设置合理的过期时间
- 考虑缓存预热

写多读少场景

推荐策略: Write-Behind + 异步更新

适用场景:
- 日志记录系统
- 统计数据计算
- 消息队列缓冲

实现要点:
- 先更新缓存再异步更新数据库
- 批量写入提升性能
- 需要处理失败重试
- 监控数据同步延迟

高并发读写场景

推荐策略: 多级缓存 + 分布式锁

适用场景:
- 热点商品查询
- 秒杀活动系统
- 实时排行榜

实现要点:
- 本地缓存 + 分布式缓存
- 使用分布式锁防止击穿
- 热点数据永不过期
- 异步刷新机制

缓存技术选型

Redis vs Memcached

特性RedisMemcached
数据类型丰富(String、Hash、List等)简单(仅String)
持久化支持不支持
集群支持需要客户端实现
内存管理自动预分配
适用场景复杂数据结构、持久化需求简单KV缓存、高性能需求

本地缓存 vs 分布式缓存

特性本地缓存分布式缓存
访问速度极快较快
内存占用单机限制集群扩展
数据一致性单机内集群间
适用场景单机应用、热点数据分布式系统、共享数据

缓存监控指标

核心指标

  • 命中率: 目标 > 90%
  • 响应时间: 目标 < 10ms
  • 内存使用率: 目标 < 80%
  • QPS: 根据业务需求
  • 错误率: 目标 < 0.1%

监控工具

  • Redis: redis-cli, Redis Insight
  • Memcached: memcached-tool, stats命令
  • 自定义监控: Prometheus + Grafana
  • APM工具: New Relic, DataDog

最佳实践

缓存设计原则

  1. 缓存雪崩预防: 过期时间加随机值
  2. 缓存穿透预防: 布隆过滤器 + 空值缓存
  3. 缓存击穿预防: 分布式锁 + 永不过期
  4. 数据一致性: 合理的过期策略
  5. 性能监控: 完善的监控体系

代码示例

python
# Cache-Aside 模式实现
def get_user_info(user_id):
    # 1. 先查缓存
    cache_key = f"user:{user_id}"
    user_data = redis.get(cache_key)
    
    if user_data:
        return json.loads(user_data)
    
    # 2. 缓存未命中,查数据库
    user_data = db.query("SELECT * FROM users WHERE id = %s", user_id)
    
    if user_data:
        # 3. 写入缓存,设置过期时间
        redis.setex(cache_key, 3600, json.dumps(user_data))
    
    return user_data

# 防止缓存击穿的分布式锁
def get_hot_data(key):
    data = redis.get(key)
    if data:
        return data
    
    # 获取分布式锁
    lock_key = f"lock:{key}"
    if redis.set(lock_key, "1", nx=True, ex=10):
        try:
            # 获取锁成功,查询数据库
            data = db.query_hot_data(key)
            redis.setex(key, 3600, data)
            return data
        finally:
            redis.delete(lock_key)
    
    # 获取锁失败,等待并重试
    time.sleep(0.1)
    return get_hot_data(key)

相关技能

  • database-query-analyzer - 分析数据库查询性能
  • performance-profiler - 系统性能分析
  • architecture-analyzer - 系统架构设计
  • data-validation - 数据一致性验证

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 缓存策略与实现 AI skill do?

当设计和实现缓存系统时,分析缓存策略,优化缓存性能,解决缓存相关问题。验证缓存架构,设计缓存模式,和最佳实践。

Why use 缓存策略与实现 on TypingMind?

Because you install it once and use it with any model. 缓存策略与实现 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 缓存策略与实现 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/backend/caching-strategies. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 缓存策略与实现?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 缓存策略与实现?

As many as you like. As long as a model supports skills, you can use 缓存策略与实现 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 缓存策略与实现 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇