go-wind-plugins 提供统一的缓存接口,支持 Redis、Memcached、本地缓存等。
type Cache interface {
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
Keys(ctx context.Context, pattern string) ([]string, error)
}
| 适配器 | 导入路径 | 特点 |
|---|
| Redis | plugins/cache/redis | 分布式缓存、丰富的数据结构 |
| Memcached | plugins/cache/memcached | 高性能纯内存缓存 |
| FreeCache | plugins/cache/freecache | 进程内缓存、零 GC |
| BigCache | plugins/cache/bigcache | 进程内缓存、高吞吐 |
| Ristretto | plugins/cache/ristretto | 进程内缓存、LFU 策略 |
import redisPlugin "github.com/tx7do/go-wind-plugins/cache/redis"
cache := redisPlugin.NewClient(
redisPlugin.WithAddr("localhost:6379"),
redisPlugin.WithDB(0),
redisPlugin.WithPassword(""),
redisPlugin.WithPoolSize(50),
redisPlugin.WithMinIdleConns(10),
)
cache.Set(ctx, "user:123", []byte(`{"name":"alice"}`), 10*time.Minute)
data, err := cache.Get(ctx, "user:123")
cache.Delete(ctx, "user:123")
exists, _ := cache.Exists(ctx, "user:123")
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:"
client := redisPlugin.GetClient()
client.HSet(ctx, "user:123:profile", "name", "alice", "age", 30)
client.SAdd(ctx, "tags:123", "vip", "active")
client.ZAdd(ctx, "ranking", redis.Z{Score: 100, Member: "alice"})
import memcachedPlugin "github.com/tx7do/go-wind-plugins/cache/memcached"
cache := memcachedPlugin.NewClient(
memcachedPlugin.WithServers("localhost:11211"),
memcachedPlugin.WithTimeout(2*time.Second),
memcachedPlugin.WithMaxIdleConns(10),
)
cache:
memcached:
servers:
- "localhost:11211"
- "localhost:11212"
timeout: 2s
max_idle_conns: 10
import freecachePlugin "github.com/tx7do/go-wind-plugins/cache/freecache"
cache := freecachePlugin.New(
freecachePlugin.WithSize(256 * 1024 * 1024),
)
cache:
freecache:
size: 268435456
cache:
l1:
driver: freecache
size: 134217728
ttl: 60s
l2:
driver: redis
addr: localhost:6379
ttl: 10m
func getCached(ctx context.Context, key string) ([]byte, error) {
if data, err := l1Cache.Get(ctx, key); err == nil {
return data, nil
}
if data, err := l2Cache.Get(ctx, key); err == nil {
l1Cache.Set(ctx, key, data, 60*time.Second)
return data, nil
}
data := loadFromDB(key)
l1Cache.Set(ctx, key, data, 60*time.Second)
l2Cache.Set(ctx, key, data, 10*time.Minute)
return data, nil
}
| 场景 | 推荐 | 理由 |
|---|
| 通用分布式缓存 | Redis | 丰富的数据结构、持久化 |
| 纯内存高性能 | Memcached | 极简、极致性能 |
| 热点数据本地缓存 | FreeCache/BigCache | 零 GC、减少网络开销 |
| 多级缓存 | L1(本地) + L2(Redis) | 兼顾延迟和一致性 |