package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/redis/go-redis/v9"
)
type redisClient struct {
client *redis.Client
key string
value string
ctx context.Context
}
// 集群redis
func connRedisCluster(address []string, password string) *redis.ClusterClient {
conf := redis.ClusterOptions{
Addrs: address,
Password: password,
}
return redis.NewClusterClient(&conf)
}
// 单机redis
func connRedisSingle(addr, password string) *redis.Client {
conf := redis.Options{
Addr: addr,
Password: password,
}
return redis.NewClient(&conf)
}
func (r *redisClient) lock() (bool, error) {
ret := r.client.SetNX(r.ctx, r.key, r.value, time.Second*10)
if err := ret.Err(); err != nil {
return false, fmt.Errorf("redis: key: %s, value %s, set nx error: %v", r.key, r.value, err)
}
return ret.Val
go实现redis分布式锁
于 2024-06-28 17:59:39 首次发布