Skip to content

Commit 6a5c292

Browse files
committed
feat:lock add deadline and modify testdemo
1 parent 635a869 commit 6a5c292

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

sample/EasyCaching.Demo.Locks/Controllers/LocksController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public async Task EtcdLockingOperation(int millisecondsTimeout)
7575

7676
try
7777
{
78-
await distributedLock.LockAsync(millisecondsTimeout);
7978
if (await distributedLock.LockAsync(millisecondsTimeout))
8079
{
8180
// Simulate operation

sample/EasyCaching.Demo.Locks/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
// use etcd cache
3030
option.UseEtcd(options =>
3131
{
32-
options.Address = "http://121.196.220.148:12379";
33-
options.Timeout = 30000;
34-
options.LockMs = 3000;
32+
options.Address = "http://127.0.0.1:2379";
33+
options.Timeout = 3000;
34+
options.LockMs = 10000;
3535
options.SerializerName = "json";
3636
}).WithJson(jsonSerializerSettingsConfigure: x =>
3737
{

sample/EasyCaching.Demo.Providers/Controllers/ValuesController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[Route("api/[controller]")]
99
public class ValuesController : Controller
1010
{
11-
//1. InMemory,Memcached,Redis,SQLite,FasterKv
11+
//1. InMemory,Memcached,Redis,SQLite,FasterKv,Etcd
1212
private readonly IEasyCachingProvider _provider;
1313

1414
public ValuesController(IEasyCachingProvider provider)
@@ -38,6 +38,9 @@ public string Get(string str)
3838
case "set" :
3939
_provider.Set("demo", "123", TimeSpan.FromMinutes(1));
4040
return "seted";
41+
case "getexpiretime":
42+
var timeSpanData = _provider.GetExpiration("demo");
43+
return $"{timeSpanData.TotalSeconds}";
4144
case "remove" :
4245
_provider.Remove("demo");
4346
return "removed";
@@ -64,6 +67,9 @@ public async Task<string> GetAsync(string str)
6467
case "set":
6568
await _provider.SetAsync("demo", "123", TimeSpan.FromMinutes(1));
6669
return "seted";
70+
case "getexpiretime":
71+
var timeSpanData = _provider.GetExpiration("demo");
72+
return $"{timeSpanData.TotalSeconds}";
6773
case "remove":
6874
await _provider.RemoveAsync("demo");
6975
return "removed";

sample/EasyCaching.Demo.Providers/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ConfigureServices(IServiceCollection services)
7474
// use etcd cache
7575
option.UseEtcd(options =>
7676
{
77-
options.Address = "http://121.196.220.148:12379";
77+
options.Address = "http://127.0.0.1:2379";
7878
options.Timeout = 30000;
7979
options.SerializerName = "json";
8080
}, "e1").WithJson(jsonSerializerSettingsConfigure: x =>

src/EasyCaching.Etcd/Internal/EtcdCaching.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public EtcdCaching(
4444
this._etcdClient = new EtcdClient(connectionString: options.Address, configureChannelOptions: (x) =>
4545
{
4646
x.Credentials = ChannelCredentials.Insecure;
47+
x.LoggerFactory = loggerFactory;
4748
});
4849
//auth
4950
if (!string.IsNullOrEmpty(options.UserName) && !string.IsNullOrEmpty(options.Password))
@@ -141,10 +142,9 @@ public async Task<bool> ExistsAsync(string cacheKey)
141142
/// </summary>
142143
/// <param name="ts"></param>
143144
/// <returns></returns>
144-
private long GetRentLeaseId(TimeSpan? ts)
145+
private long GetRentLeaseId(TimeSpan? ts, CancellationTokenSource cts)
145146
{
146147
// create rent id to bind
147-
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
148148
var response = _etcdClient.LeaseGrant(request: new LeaseGrantRequest()
149149
{
150150
TTL = (long)(ts.Value.TotalMilliseconds < 1000 ? 1: ts.Value.TotalMilliseconds / 1000),
@@ -164,7 +164,7 @@ private async Task<long> GetRentLeaseIdAsync(TimeSpan? ts)
164164
var response = await _etcdClient.LeaseGrantAsync(request: new LeaseGrantRequest()
165165
{
166166
TTL = (long)(ts.Value.TotalMilliseconds < 1000 ? 1 : ts.Value.TotalMilliseconds / 1000),
167-
}, cancellationToken: cts.Token);
167+
}, deadline: DateTime.UtcNow.AddMilliseconds(_options.Timeout), cancellationToken: cts.Token);
168168
return response.ID;
169169
}
170170

@@ -179,8 +179,8 @@ public bool Set<T>(string key, T value, TimeSpan? ts)
179179
{
180180
try
181181
{
182-
long leaseId = ts.HasValue ? GetRentLeaseId(ts) : 0;
183182
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
183+
long leaseId = ts.HasValue ? GetRentLeaseId(ts,cts) : 0;
184184
PutRequest request = new PutRequest()
185185
{
186186
Key = ByteString.CopyFromUtf8(key),
@@ -236,14 +236,14 @@ public bool Lock(string key, TimeSpan? ts)
236236
{
237237
try
238238
{
239-
long leaseId = ts.HasValue ? GetRentLeaseId(ts) : 0;
240239
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
240+
long leaseId = ts.HasValue ? GetRentLeaseId(ts,cts) : 0;
241241
LockRequest request = new LockRequest()
242242
{
243243
Name = ByteString.CopyFromUtf8(key),
244244
Lease = leaseId
245245
};
246-
var response = _etcdClient.Lock(request: request, headers: _metadata, deadline: DateTime.UtcNow.AddSeconds(_options.Timeout), cancellationToken: cts.Token);
246+
var response = _etcdClient.Lock(request: request, headers: _metadata, deadline: DateTime.UtcNow.AddMilliseconds(_options.Timeout), cancellationToken: cts.Token);
247247
if (response?.Key == null || response.Key.IsEmpty)
248248
{
249249
return false;
@@ -275,14 +275,14 @@ public async Task<bool> LockAsync(string key, TimeSpan? ts)
275275
{
276276
try
277277
{
278-
long leaseId = ts.HasValue ? GetRentLeaseId(ts) : 0;
279278
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
279+
long leaseId = ts.HasValue ? GetRentLeaseId(ts,cts) : 0;
280280
LockRequest request = new LockRequest()
281281
{
282282
Name = ByteString.CopyFromUtf8(key),
283283
Lease = leaseId
284284
};
285-
var response = await _etcdClient.LockAsync(request: request, headers: _metadata,deadline: DateTime.UtcNow.AddSeconds(_options.Timeout), cancellationToken: cts.Token);
285+
var response = await _etcdClient.LockAsync(request: request, headers: _metadata, deadline: DateTime.UtcNow.AddMilliseconds(_options.Timeout), cancellationToken: cts.Token);
286286
if (response?.Key == null || response.Key.IsEmpty)
287287
{
288288
return false;
@@ -315,7 +315,7 @@ public bool UnLock(string key)
315315
try
316316
{
317317
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
318-
var response = _etcdClient.Unlock(key, headers: _metadata, cancellationToken: cts.Token);
318+
var response = _etcdClient.Unlock(key, headers: _metadata, deadline: DateTime.UtcNow.AddMilliseconds(_options.Timeout), cancellationToken: cts.Token);
319319
return true;
320320
}
321321
catch (Exception ex)
@@ -335,7 +335,7 @@ public async Task<bool> UnLockAsnyc(string key)
335335
try
336336
{
337337
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(_options.Timeout));
338-
var response = await _etcdClient.UnlockAsync(key, headers: _metadata, cancellationToken: cts.Token);
338+
var response = await _etcdClient.UnlockAsync(key, headers: _metadata, deadline: DateTime.UtcNow.AddMilliseconds(_options.Timeout), cancellationToken: cts.Token);
339339
return true;
340340
}
341341
catch (Exception ex)

0 commit comments

Comments
 (0)