Skip to content

Faster locking on .NET 9.0+ using System.Threading.Lock #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions FastCache/FastCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class FastCache<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>,
{
private readonly ConcurrentDictionary<TKey, TtlValue> _dict = new ConcurrentDictionary<TKey, TtlValue>();

private readonly Lock _lock = new();
private readonly Timer _cleanUpTimer;
private readonly EvictionCallback _itemEvicted;

Expand Down Expand Up @@ -61,7 +62,7 @@ await _globalStaticLock.WaitAsync()
public void EvictExpired()
{
//Eviction already started by another thread? forget it, lets move on
if (Monitor.TryEnter(_cleanUpTimer)) //use the timer-object for our lock, it's local, private and instance-type, so its ok
if (_lock.TryEnter()) //use the timer-object for our lock, it's local, private and instance-type, so its ok
{
List<TKey> evictedKeys = null; // Batch eviction callbacks
try
Expand All @@ -86,7 +87,7 @@ public void EvictExpired()
}
finally
{
Monitor.Exit(_cleanUpTimer);
_lock.Exit();
}

// Trigger batched eviction callbacks outside the loop to prevent flooding the thread pool
Expand Down
58 changes: 33 additions & 25 deletions FastCache/FastCache.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<PackageId>Jitbit.FastCache</PackageId>
<Title>FastCache</Title>
<Authors>Alex from Jitbit</Authors>
<Product>FastCache</Product>
<PackageProjectUrl>https://github.com/jitbit/fastcache</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/jitbit/fastcache</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.1.0</Version>
<PackageTags>cache;caching;MemoryCache</PackageTags>
<Description>Fastest in-memoery cache for .NET</Description>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<PackageId>Jitbit.FastCache</PackageId>
<Title>FastCache</Title>
<Authors>Alex from Jitbit</Authors>
<Product>FastCache</Product>
<PackageProjectUrl>https://github.com/jitbit/fastcache</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/jitbit/fastcache</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.1.0</Version>
<PackageTags>cache;caching;MemoryCache</PackageTags>
<Description>Fastest in-memoery cache for .NET</Description>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Include="Backport.System.Threading.Lock" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>