本着学习和实践的目标, 基于 100% 纯度 go 语言实现的缓存读写一致性 lib 框架,实现到的功能点包括:
- 缓存一致性保证
- 写流程: 设置禁用写缓存标识 -> 删除缓存 -> 写数据库 -> 延时启用写缓存标识
func Test_consistent_Cache(t *testing.T) {
service := consistent_cache.NewService(
// 缓存模块
redis.NewRedisCache(&redis.Config{
// redis 地址
Address: redisAddress,
// redis 密码
Password: redisPassword,
}),
// 数据库模块
mysql.NewDB(mysqlDSN),
// 缓存过期时长 120s
consistent_cache.WithCacheExpireSeconds(120),
// 缓存过期时间添加随机扰动系数,防缓存雪崩
consistent_cache.WithCacheExpireRandomMode(),
// 写缓存禁用机制延时 1s 启用
consistent_cache.WithDisableExpireSeconds(1),
)
ctx := context.Background()
exp := Example{
Key_: "test",
Data: "test",
}
// 写操作
if err := service.Put(ctx, &exp); err != nil {
t.Error(err)
return
}
// 读操作
expReceiver := Example{
Key_: "test",
}
if _, err := service.Get(ctx, &expReceiver); err != nil {
t.Error(err)
return
}
// 读取到的数据结果 以及是否使用到缓存
t.Logf("read data: %s, ", expReceiver.Data)
}