-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathLocksController.cs
More file actions
99 lines (91 loc) · 2.51 KB
/
Copy pathLocksController.cs
File metadata and controls
99 lines (91 loc) · 2.51 KB
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
namespace EasyCaching.Demo.Locks.Controllers;
[Route("api/[controller]")]
public class LocksController : Controller
{
private readonly IDistributedLockFactory _distributedLockFactory;
public LocksController(IDistributedLockFactory distributedLockFactory)
{
_distributedLockFactory = distributedLockFactory;
}
[HttpPost("distributed-locking")]
public async Task DistributedLockingOperation(int millisecondsTimeout)
{
using var distributedLock = _distributedLockFactory.CreateLock("DefaultRedis", "YourKey");
try
{
if (await distributedLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)
{
// log error
throw new Exception("Exception", ex);
}
finally
{
// release lock at the end
await distributedLock.ReleaseAsync();
}
}
[HttpPost("memory-locking")]
public async Task MemoryLockingOperation(int millisecondsTimeout)
{
using var memoryLock = _distributedLockFactory.CreateLock("DefaultInMemory", "YourKey");
try
{
if (await memoryLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)
{
// log error
throw new Exception("Exception", ex);
}
finally
{
// release lock at the end
await memoryLock.ReleaseAsync();
}
}
[HttpPost("etcd-locking")]
public async Task EtcdLockingOperation(int millisecondsTimeout)
{
using var distributedLock = _distributedLockFactory.CreateLock("DefaultEtcd", "YourKey");
try
{
if (await distributedLock.LockAsync(millisecondsTimeout))
{
// Simulate operation
Thread.Sleep(2000);
}
else
{
// Proper error
}
}
catch (Exception ex)
{
// log error
throw new Exception("Exception", ex);
}
finally
{
// release lock at the end
await distributedLock.ReleaseAsync();
}
}
}