分布式锁
1. 简介
2. 安装
go get github.com/redis/go-redis/v9go get github.com/sagoo-cloud/nexframe3. 基本用法
3.1 创建 Nx 实例
import (
"github.com/redis/go-redis/v9"
"github.com/sagoo-cloud/nexframe/os/nx"
)
func main() {
// 创建 Redis 客户端
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
// 创建 Nx 实例
nxLock, err := nx.New(
nx.WithRedis(redisClient),
nx.WithKey("my-lock-key"),
)
if err != nil {
panic(err)
}
// 使用 nxLock ...
}3.2 获取锁
3.3 释放锁
4. 高级配置
4.1 设置过期时间
4.2 配置重试策略
5. 最佳实践
6. 错误处理
7. 性能考虑
8. 限制和注意事项
9. 示例:在 Web 服务中使用 Nx
10. 结论
最后更新于