xiazhengtongxue
9 天以前 cbfc5ac3c45eff75e912ba6aeaad83533a1a4296
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
using WIDESEAWCS_RedisService.Connection;
using WIDESEAWCS_RedisService.Options;
 
namespace WIDESEAWCS_RedisService.Lock
{
    public class RedisDistributedLockService : IDistributedLockService
    {
        private readonly IRedisConnectionManager _connectionManager;
        private readonly RedisOptions _options;
        private readonly ILogger<RedisDistributedLockService> _logger;
 
        // Lua脚本:安全释放锁(只有持有者才能释放)
        private const string ReleaseLockScript = @"
            if redis.call('get', KEYS[1]) == ARGV[1] then
                return redis.call('del', KEYS[1])
            else
                return 0
            end";
 
        // Lua脚本:安全续期锁
        private const string RenewLockScript = @"
            if redis.call('get', KEYS[1]) == ARGV[1] then
                return redis.call('pexpire', KEYS[1], ARGV[2])
            else
                return 0
            end";
 
        public RedisDistributedLockService(
            IRedisConnectionManager connectionManager,
            IOptions<RedisOptions> options,
            ILogger<RedisDistributedLockService> logger)
        {
            _connectionManager = connectionManager;
            _options = options.Value;
            _logger = logger;
        }
 
        private string BuildKey(string key) => $"{_options.KeyPrefix}lock:{key}";
 
        public string? AcquireLock(string lockKey, TimeSpan expiry, TimeSpan? waitTimeout = null)
        {
            var fullKey = BuildKey(lockKey);
            var token = Guid.NewGuid().ToString("N");
            var db = _connectionManager.GetDatabase();
            var deadline = DateTime.UtcNow + (waitTimeout ?? TimeSpan.Zero);
 
            do
            {
                if (db.StringSet(fullKey, token, expiry, When.NotExists))
                    return token;
 
                if (waitTimeout == null) return null;
                Thread.Sleep(50);
            }
            while (DateTime.UtcNow < deadline);
 
            return null;
        }
 
        public bool ReleaseLock(string lockKey, string lockToken)
        {
            var db = _connectionManager.GetDatabase();
            var result = db.ScriptEvaluate(
                ReleaseLockScript,
                new RedisKey[] { BuildKey(lockKey) },
                new RedisValue[] { lockToken });
            return (long)result == 1;
        }
 
        public bool RenewLock(string lockKey, string lockToken, TimeSpan expiry)
        {
            var db = _connectionManager.GetDatabase();
            var result = db.ScriptEvaluate(
                RenewLockScript,
                new RedisKey[] { BuildKey(lockKey) },
                new RedisValue[] { lockToken, (long)expiry.TotalMilliseconds });
            return (long)result == 1;
        }
 
        public T? ExecuteWithLock<T>(string lockKey, TimeSpan expiry, Func<T> action, TimeSpan? waitTimeout = null)
        {
            var token = AcquireLock(lockKey, expiry, waitTimeout);
            if (token == null) return default;
            try
            {
                return action();
            }
            finally
            {
                ReleaseLock(lockKey, token);
            }
        }
 
        public bool ExecuteWithLock(string lockKey, TimeSpan expiry, Action action, TimeSpan? waitTimeout = null)
        {
            var token = AcquireLock(lockKey, expiry, waitTimeout);
            if (token == null) return false;
            try
            {
                action();
                return true;
            }
            finally
            {
                ReleaseLock(lockKey, token);
            }
        }
    }
}