Skip to content

Commit d6203cc

Browse files
Performance improvement for .NET 9.0+
1 parent c9cafa1 commit d6203cc

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

bus/EasyCaching.Bus.Zookeeper/DefaultZookeeperBus.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
namespace EasyCaching.Bus.Zookeeper
1+
using EasyCaching.Core;
2+
using EasyCaching.Core.Bus;
3+
using EasyCaching.Core.Serialization;
4+
using Microsoft.Extensions.Options;
5+
using org.apache.zookeeper;
6+
using org.apache.zookeeper.data;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Threading;
12+
using System.Threading.Tasks;
13+
14+
namespace EasyCaching.Bus.Zookeeper
215
{
3-
using EasyCaching.Core;
4-
using EasyCaching.Core.Bus;
5-
using EasyCaching.Core.Serialization;
6-
using Microsoft.Extensions.Options;
7-
using org.apache.zookeeper;
8-
using org.apache.zookeeper.data;
9-
using System;
10-
using System.Collections.Generic;
11-
using System.Linq;
12-
using System.Text;
13-
using System.Threading;
14-
using System.Threading.Tasks;
15-
1616
public class DefaultZookeeperBus : EasyCachingAbstractBus
1717
{
1818
/// <summary>
@@ -41,7 +41,7 @@ public class DefaultZookeeperBus : EasyCachingAbstractBus
4141
/// <summary>
4242
/// lock
4343
/// </summary>
44-
private readonly object _zkEventLock = new object();
44+
private readonly Lock _zkEventLock = LockFactory.Create();
4545

4646
/// <summary>
4747
/// The serializer.
@@ -214,7 +214,7 @@ private async Task SubscribeDataChange(WatchedEvent @event)
214214
/// <returns></returns>
215215
private async Task ReZkConnect()
216216
{
217-
if (!Monitor.TryEnter(_zkEventLock, _zkBusOptions.ConnectionTimeout))
217+
if (!_zkEventLock.TryEnter(_zkBusOptions.ConnectionTimeout))
218218
return;
219219
try
220220
{
@@ -234,7 +234,7 @@ private async Task ReZkConnect()
234234
}
235235
finally
236236
{
237-
Monitor.Exit(_zkEventLock);
237+
_zkEventLock.Exit();
238238
}
239239
}
240240

bus/EasyCaching.Bus.Zookeeper/EasyCaching.Bus.Zookeeper.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<Import Project="../../build/version.props" />
44
<Import Project="../../build/releasenotes.props" />
55
<PropertyGroup>
6-
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
6+
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
7+
<LangVersion>latest</LangVersion>
78
<Owners>ncc;Catcher Wong</Owners>
89
<Authors>ncc;Catcher Wong</Authors>
910
<VersionPrefix>$(EasyCachingZookeeperBusPackageVersion)</VersionPrefix>
@@ -40,4 +41,11 @@
4041
<PackageReference Include="ZooKeeperNetEx" Version="3.4.12.4" />
4142
</ItemGroup>
4243

44+
<ItemGroup>
45+
<PackageReference Include="Backport.System.Threading.Lock" Version="2.0.7" />
46+
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
47+
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
48+
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
49+
</ItemGroup>
50+
4351
</Project>

src/EasyCaching.Core/DistributedLock/DistributedLock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace EasyCaching.Core.DistributedLock
99
public class DistributedLock : MemoryLock
1010
{
1111
private readonly IDistributedLockProvider _provider;
12-
private readonly object _syncObj = new object();
12+
private readonly Lock _syncObj = LockFactory.Create();
1313
private readonly DistributedLockOptions _options;
1414
private readonly ILogger _logger;
1515

src/EasyCaching.Core/DistributedLock/MemoryLock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class MemoryLock : IDistributedLock
1616

1717
public string Key { get; }
1818

19-
private readonly object _syncObj = new object();
19+
private readonly Lock _syncObj = LockFactory.Create();
2020

2121
public MemoryLock(string key) => Key = key;
2222

src/EasyCaching.Core/EasyCaching.Core.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<Import Project="../../build/version.props" />
33
<Import Project="../../build/releasenotes.props" />
44
<PropertyGroup>
5-
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
6+
<LangVersion>latest</LangVersion>
67
<Owners>ncc;Catcher Wong</Owners>
78
<Authors>ncc;Catcher Wong</Authors>
89
<VersionPrefix>$(EasyCachingCorePackageVersion)</VersionPrefix>
@@ -44,8 +45,21 @@
4445
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
4546
</ItemGroup>
4647

48+
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
49+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
50+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
51+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
52+
</ItemGroup>
53+
4754
<ItemGroup>
4855
<PackageReference Include="AsyncKeyedLock" Version="7.0.1" />
4956
</ItemGroup>
57+
58+
<ItemGroup>
59+
<PackageReference Include="Backport.System.Threading.Lock" Version="2.0.7" />
60+
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
61+
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
62+
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
63+
</ItemGroup>
5064

5165
</Project>

0 commit comments

Comments
 (0)