GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

缓存插件

go-wind-plugins 提供统一的缓存接口 cache.Cache,内置本地缓存与 Redis 两个插件。

一、Cache 接口

type Cache interface {
    Get(ctx context.Context, key string) ([]byte, error)               // 未命中/过期返回 ErrNotFound
    Set(ctx context.Context, key string, value []byte, ttl time.Duration) error  // 零 TTL 表示永不过期
    SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error) // 分布式锁/防击穿原语
    Delete(ctx context.Context, key string) error
    Has(ctx context.Context, key string) (bool, error)
    GetMulti(ctx context.Context, keys []string) ([][]byte, error)
    SetMulti(ctx context.Context, items []Item) error
    Close() error
}

二、适配器列表

适配器导入路径引擎
Localplugins/cache/local本地内存缓存(FreeCache 环形缓冲,LRU 淘汰)
Redisplugins/cache/redisgo-redis v9(UniversalClient,单机/集群/哨兵)

三、Redis

应用侧创建并持有原生 go-redis 客户端,插件将其包装为统一的 cache.Cache;WithKeyPrefix 为共享实例上的键做命名空间隔离:

import (
    goredis "github.com/redis/go-redis/v9"
    "github.com/tx7do/go-wind-plugins/cache/redis"
)

client := goredis.NewClient(&goredis.Options{
    Addr: "localhost:6379",
})
defer client.Close()

cache := redis.New(client,
    redis.WithKeyPrefix("myapp:"),
)
defer cache.Close()

基本操作

// Set
cache.Set(ctx, "user:123", []byte(`{"name":"alice"}`), 10*time.Minute)

// Get
data, err := cache.Get(ctx, "user:123")

// Has / Delete
exists, _ := cache.Has(ctx, "user:123")
cache.Delete(ctx, "user:123")

// SetNX:分布式锁与防击穿原语
ok, _ := cache.SetNX(ctx, "lock:order:123", []byte("locked"), 30*time.Second)

// 批量读写(翻译为原生单次往返命令)
_ = cache.SetMulti(ctx, []cache.Item{
    {Key: "user:2", Value: []byte("bob"), TTL: 5 * time.Minute},
})
_, _ = cache.GetMulti(ctx, []string{"user:2", "user:3"})

YAML 配置

cache:
  redis:
    addr: "localhost:6379"
    password: ${REDIS_PASSWORD}
    db: 0
    pool_size: 50
    min_idle_conns: 10
    max_retries: 3
    read_timeout: 3s
    write_timeout: 3s
    key_prefix: "myapp:"

四、Local(本地缓存)

基于 FreeCache 的进程内缓存——预分配环形缓冲、LRU 淘汰、零 GC 停顿,无需外部服务:

import "github.com/tx7do/go-wind-plugins/cache/local"

c := local.New(
    local.WithSize(100 * 1024 * 1024),   // 100 MB
    local.WithDefaultTTL(5*time.Minute), // 默认 TTL
)
defer c.Close()

_ = c.Set(ctx, "user:1", []byte("alice"), 10*time.Minute)
_, _ = c.Get(ctx, "user:1")

YAML 配置

cache:
  local:
    size: 268435456        # 256 MB in bytes
    default_ttl: 5m

五、多级缓存(Local + Redis 组合)

cache:
  l1:                    # 本地缓存 (L1)
    driver: local
    size: 134217728      # 128 MB
    ttl: 60s
  l2:                    # 分布式缓存 (L2)
    driver: redis
    addr: localhost:6379
    ttl: 10m
// 多级缓存包装器
func getCached(ctx context.Context, key string) ([]byte, error) {
    // L1: 本地缓存
    if data, err := l1Cache.Get(ctx, key); err == nil {
        return data, nil
    }

    // L2: Redis
    if data, err := l2Cache.Get(ctx, key); err == nil {
        l1Cache.Set(ctx, key, data, 60*time.Second)
        return data, nil
    }

    // DB 回源
    data := loadFromDB(key)
    l1Cache.Set(ctx, key, data, 60*time.Second)
    l2Cache.Set(ctx, key, data, 10*time.Minute)
    return data, nil
}

六、选择指南

场景推荐理由
通用分布式缓存Redis丰富的数据结构、持久化
热点数据本地缓存Local零 GC、无网络开销
分布式锁 / 防击穿Redis SetNX原生支持 SetNX 原语
批量读写Redis GetMulti/SetMulti单次往返命令
多级缓存Local(L1) + Redis(L2)兼顾延迟和一致性

相关文档

  • 插件配置系统
  • 插件总览
  • 数据库插件
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
链路追踪插件
Next
对象存储插件(OSS)