/matex-skills:Matex-Setup-Cachefrom matex-skills
Implements cache-aside pattern with concurrent miss coalescing (anti-crash), TTL jitter (anti-thundering herd), and write-through invalidation for the Matex project.
Ships in matex-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
用 pkg/core/cache(不是直接 redis.GetJSON/SetJSON)—— 它补的是两个只在压力下才
暴露的失效模式:并发 miss 一起回源(击穿)和一批 key 同时过期(雪崩)。
u, err := cache.GetOrLoad(ctx, s.redis, cache.Key("user", id),
cache.Config{TTL: 30 * time.Second, Jitter: 5 * time.Second},
func(ctx context.Context) (User, error) {
return s.dao.ByID(ctx, q, id) // 回源:DB / 下游 / 计算
})
一次调用做四件事:查缓存 → 命中返回 → miss 时按 key 合并并发请求(只回源一次)→ 回源结果按抖动后的 TTL 写入。
Jitter 取 TTL 的 10~20%;Jitter >= TTL 会被压到 TTL/2(不会抖成 0)。s.redis 在 Setup 里用 if rc, ok := env.Redis(); ok { s.redis = rc } 取;为 nil 时直接回源不缓存 —— 调用点不写 if env.Redis != nil。回源跑在第一个调用者的 ctx 下:别挂在一个随时取消的短 ctx 上,否则一组请求会一起挂。
// db 句柄在 Setup 里用 env.RequireDB()(或 env.DB() 逗号-ok)取好,存到 s.db / s.dao。
if _, err := s.dao.Update(ctx, s.db, id, in); err != nil {
return nil, err
}
if err := cache.Invalidate(ctx, s.redis, cache.Key("user", id)); err != nil {
obs.Warn(ctx, "invalidate failed", "id", id, "err", err)
}
删缓存,不要更新缓存:并发写下"写 DB + 写缓存"存在旧值覆盖新值的窗口;"写 DB + 删缓存"任何顺序都安全(下次读必然回源)。
失效失败不要让写请求失败,但要记 warn —— 它意味着 TTL 内可能读到旧值。TTL 就是 "最坏情况的脏读窗口"。
cache.Key("user", 42) // "user:42"
cache.Key("order", id, "items") // "order:9:items"
必须带命名空间:缓存和其它 redis 数据共用一个库,而 GetOrLoad 按字面 key 合并
—— 两个功能都用 "1" 会互相串。
Invalidate。rc := redistest.Start(t) // miniredis 进程内
svc.redis = rc
要断言"并发 miss 只回源一次",回源函数里加计数器,并让它在 channel 上阻塞到所有
goroutine 都进入 —— 参考 examples/cache 的 TestConcurrentMissesCollapse。
go test ./pkg/core/cache ./examples/cache
make ci
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub smallnest/matex --plugin matex-skills15 skills in this plugin
/Matex-Add-DomainMatex Add Domain在 matex 脚手架中新增一个业务域(handler/service/dao + 路由注册 + 装配 + 测试)。当用户说"新增 domain / 加一个业务域 / 新增模块 / add domain / new domain / 建一个 xx 服务"时使用。/Matex-Setup-Audit-LogMatex Setup Audit Log在 matex 中记录审计日志:pkg/core/audit 的 Record/Event(actor/action/resource/result/detail),默认写结构化日志,可用 SetSink 落库或投递到只追加的存储。当用户说"审计 / 审计日志 / audit log / 操作记录 / 谁改了什么 / 合规 / 不可否认 / access log"时使用。/Matex-Setup-AuthMatex Setup Auth在 matex 中接入认证与授权:配置 auth 段、验签 Bearer token、按路由分级保护、读取当前主体。当用户说"加认证 / 加鉴权 / 接 auth / setup auth / JWT / OIDC / 登录校验 / token 校验 / 权限控制 / RBAC"时使用。/Matex-Setup-CronMatex Setup Cron在 matex 中加定时任务:固定周期 / cron 表达式 / 一次性任务,多副本下用 redis 选主保证每个周期(槽)只跑一次。当用户说"定时任务 / 定时器 / cron / cron 表达式 / 周期任务 / 后台任务 / 跑批 / 一次性任务 / one-shot / 延迟执行 / 选主 / leader election / 只能跑一次 / schedule"时使用。/Matex-Setup-DatabaseMatex Setup Database在 matex 中接入 SQL 数据库(PostgreSQL / MySQL / SQLite):配置 db 段、写迁移、写 DAO、跑测试。当用户说"接数据库 / 配 postgres / 配 mysql / 配 sqlite / setup database / 加数据库 / 连数据库 / 写表 / 迁移 / DAO"时使用。/Matex-Setup-Feature-FlagsMatex Setup Feature Flags在 matex 中用功能开关(feature flag)按部署/运行时切换代码路径:pkg/core/feature 的 Register/Enabled/Set/Configure,config.yaml 的 feature 段与 FEATURE_ 环境变量。当用户说"功能开关 / feature flag / 灰度 / 开关 / 按环境打开 / kill switch / 紧急关闭 / toggles"时使用。