acquireLock 尝试获取分布式锁
(ctx context.Context, lockKey, lockValue string, ttl time.Duration)
| 301 | |
| 302 | // acquireLock 尝试获取分布式锁 |
| 303 | func (c *lotteryDrawCache) acquireLock(ctx context.Context, lockKey, lockValue string, ttl time.Duration) (bool, error) { |
| 304 | // 使用 SET 命令获取锁 |
| 305 | ok, err := c.client.SetNX(ctx, lockKey, lockValue, ttl).Result() |
| 306 | if err != nil { |
| 307 | return false, err |
| 308 | } |
| 309 | |
| 310 | return ok, nil |
| 311 | } |
| 312 | |
| 313 | // releaseLock 释放分布式锁,使用 Lua 脚本确保操作的原子性 |
| 314 | func (c *lotteryDrawCache) releaseLock(ctx context.Context, lockKey, lockValue string) error { |
no test coverage detected