Skip to content

Commit 5b16024

Browse files
committed
Use the new locker methods
1 parent 47b3e52 commit 5b16024

File tree

7 files changed

+223
-592
lines changed

7 files changed

+223
-592
lines changed

src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>Entity Framework Core Second Level Caching Library.</Description>
4-
<VersionPrefix>4.8.6</VersionPrefix>
4+
<VersionPrefix>4.8.7</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/EFCoreSecondLevelCacheInterceptor/ILockProvider.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using AsyncKeyedLock;
2-
using System;
1+
using System;
32
using System.Threading;
43
using System.Threading.Tasks;
54

@@ -13,10 +12,10 @@ public interface ILockProvider : IDisposable
1312
/// <summary>
1413
/// Tries to enter the sync lock
1514
/// </summary>
16-
AsyncNonKeyedLockReleaser Lock(CancellationToken cancellationToken = default);
15+
IDisposable? Lock(CancellationToken cancellationToken = default);
1716

1817
/// <summary>
1918
/// Tries to enter the async lock
2019
/// </summary>
21-
ValueTask<AsyncNonKeyedLockReleaser> LockAsync(CancellationToken cancellationToken = default);
20+
ValueTask<IDisposable?> LockAsync(CancellationToken cancellationToken = default);
2221
}

src/EFCoreSecondLevelCacheInterceptor/LockProvider.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@ namespace EFCoreSecondLevelCacheInterceptor;
1212
public sealed class LockProvider : ILockProvider
1313
{
1414
private readonly AsyncNonKeyedLocker _lock = new();
15+
private readonly TimeSpan _timeout = TimeSpan.FromSeconds(value: 7);
1516

1617
/// <summary>
1718
/// Tries to enter the sync lock
1819
/// </summary>
1920
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20-
public AsyncNonKeyedLockReleaser Lock(CancellationToken cancellationToken = default) => _lock.Lock(cancellationToken);
21+
public IDisposable? Lock(CancellationToken cancellationToken = default)
22+
=> _lock.LockOrNull(_timeout, cancellationToken);
2123

2224
/// <summary>
2325
/// Tries to enter the async lock
2426
/// </summary>
2527
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26-
public ValueTask<AsyncNonKeyedLockReleaser> LockAsync(CancellationToken cancellationToken = default) =>
27-
_lock.LockAsync(cancellationToken);
28+
public ValueTask<IDisposable?> LockAsync(CancellationToken cancellationToken = default)
29+
=> _lock.LockOrNullAsync(_timeout, cancellationToken);
2830

2931
/// <summary>
3032
/// Disposes the lock
31-
/// </summary>
32-
public void Dispose()
33-
{
34-
_lock.Dispose();
35-
}
33+
/// </summary>
34+
public void Dispose() => _lock.Dispose();
3635
}

src/Tests/EFCoreSecondLevelCacheInterceptor.UnitTests/LockProviderTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using AsyncKeyedLock;
2-
31
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
42

53
public class LockProviderTests
@@ -14,7 +12,7 @@ public void Lock_ReturnsNonNullReleaser()
1412
var releaser = lockProvider.Lock();
1513

1614
// Assert
17-
Assert.IsType<AsyncNonKeyedLockReleaser>(releaser);
15+
Assert.IsAssignableFrom<IDisposable>(releaser);
1816
}
1917

2018
[Fact]
@@ -27,7 +25,7 @@ public async Task LockAsync_ReturnsNonNullReleaser()
2725
var releaser = await lockProvider.LockAsync();
2826

2927
// Assert
30-
Assert.IsType<AsyncNonKeyedLockReleaser>(releaser);
28+
Assert.IsAssignableFrom<IDisposable>(releaser);
3129
}
3230

3331
[Fact]
@@ -38,10 +36,10 @@ public void Lock_CanBeDisposed()
3836
var releaser = lockProvider.Lock();
3937

4038
// Act
41-
releaser.Dispose();
39+
releaser?.Dispose();
4240

4341
// Assert
44-
Assert.True(true); // If no exception is thrown, the test passes
42+
Assert.True(condition: true); // If no exception is thrown, the test passes
4543
}
4644

4745
[Fact]
@@ -52,10 +50,10 @@ public async Task LockAsync_CanBeDisposed()
5250
var releaser = await lockProvider.LockAsync();
5351

5452
// Act
55-
releaser.Dispose();
53+
releaser?.Dispose();
5654

5755
// Assert
58-
Assert.True(true); // If no exception is thrown, the test passes
56+
Assert.True(condition: true); // If no exception is thrown, the test passes
5957
}
6058

6159
[Fact]
@@ -68,6 +66,6 @@ public void Dispose_DisposesLockProvider()
6866
lockProvider.Dispose();
6967

7068
// Assert
71-
Assert.True(true); // If no exception is thrown, the test passes
69+
Assert.True(condition: true); // If no exception is thrown, the test passes
7270
}
7371
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace EFCoreSecondLevelCacheInterceptor.UnitTests;
2+
3+
public class MockDisposable : IDisposable
4+
{
5+
private bool _disposed;
6+
7+
public void Dispose()
8+
{
9+
Dispose(disposing: true);
10+
11+
// tell the GC not to finalize
12+
GC.SuppressFinalize(this);
13+
}
14+
15+
protected virtual void Dispose(bool disposing)
16+
{
17+
if (!_disposed)
18+
{
19+
if (disposing)
20+
{
21+
// Not in destructor, OK to reference other objects
22+
}
23+
24+
// perform cleanup for this object
25+
}
26+
27+
_disposed = true;
28+
}
29+
30+
~MockDisposable() => Dispose(disposing: false);
31+
}

0 commit comments

Comments
 (0)