Skip to content

Commit 1a8c277

Browse files
committed
Add additional target .netstandard2.0 +semver: minor
1 parent 1f8eb2f commit 1a8c277

7 files changed

Lines changed: 67 additions & 7 deletions

File tree

AsyncMemoryCache/AsyncMemoryCache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public sealed class AsyncMemoryCache<TKey, TValue> : IAsyncDisposable, IAsyncMem
3030

3131
public AsyncMemoryCache(IAsyncMemoryCacheConfiguration<TKey, TValue> configuration, ILogger<AsyncMemoryCache<TKey, TValue>>? logger = null)
3232
{
33+
#if NET8_0_OR_GREATER
3334
ArgumentNullException.ThrowIfNull(configuration);
35+
#else
36+
if (configuration is null)
37+
throw new ArgumentNullException(nameof(configuration));
38+
#endif
3439

3540
_configuration = configuration;
3641
_cache = configuration.CacheBackingStore;

AsyncMemoryCache/AsyncMemoryCache.csproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
55
<Nullable>enable</Nullable>
66

7+
<LangVersion>latest</LangVersion>
8+
79
<EnableNETAnalyzers>true</EnableNETAnalyzers>
810
<AnalysisLevel>preview</AnalysisLevel>
911

@@ -34,10 +36,17 @@
3436
</Description>
3537
</PropertyGroup>
3638

39+
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
40+
<PackageReference Include="PolySharp" Version="1.14.1">
41+
<PrivateAssets>all</PrivateAssets>
42+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
43+
</PackageReference>
44+
</ItemGroup>
45+
3746
<ItemGroup>
3847
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
3948
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.1.2" />
40-
49+
4150
<PackageReference Include="Roslynator.Analyzers" Version="4.10.0">
4251
<PrivateAssets>all</PrivateAssets>
4352
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

AsyncMemoryCache/CacheEntityReference.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ public sealed class CacheEntityReference<TKey, TValue> : IDisposable
1414

1515
public CacheEntityReference(CacheEntity<TKey, TValue> cacheEntity)
1616
{
17+
#if NET8_0_OR_GREATER
1718
ArgumentNullException.ThrowIfNull(cacheEntity);
19+
#else
20+
if (cacheEntity is null)
21+
throw new ArgumentNullException(nameof(cacheEntity));
22+
#endif
1823

1924
CacheEntity = cacheEntity;
2025
_ = Interlocked.Increment(ref cacheEntity.References);
2126
}
2227

28+
#if NET8_0_OR_GREATER
2329
[ExcludeFromCodeCoverage(Justification = "Finalizers are unreliable in tests")]
30+
#endif
2431
~CacheEntityReference()
2532
{
2633
Dispose();

AsyncMemoryCache/EvictionBehaviors/DefaultEvictionBehavior.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,34 @@ namespace AsyncMemoryCache.EvictionBehaviors;
88

99
public sealed class DefaultEvictionBehavior : IEvictionBehavior
1010
{
11-
private readonly PeriodicTimer _timer;
1211
private readonly CancellationTokenSource _cts;
12+
private readonly TimeSpan _interval;
13+
#if NET8_0_OR_GREATER
14+
private readonly PeriodicTimer _timer;
1315
private Task? _workerTask;
1416

1517
public DefaultEvictionBehavior(TimeProvider? timeProvider = default, TimeSpan? evictionCheckInterval = default)
1618
{
17-
var interval = evictionCheckInterval ?? TimeSpan.FromSeconds(30);
18-
_timer = new(interval, timeProvider ?? TimeProvider.System);
19+
_interval = evictionCheckInterval ?? TimeSpan.FromSeconds(30);
20+
_timer = new(_interval, timeProvider ?? TimeProvider.System);
21+
_cts = new();
22+
}
23+
#else
24+
private Timer? _timer;
25+
26+
public DefaultEvictionBehavior(TimeSpan? evictionCheckInterval = default)
27+
{
1928
_cts = new();
29+
_interval = evictionCheckInterval ?? TimeSpan.FromSeconds(30);
2030
}
31+
#endif
2132

2233
public void Start<TKey, TValue>(IAsyncMemoryCacheConfiguration<TKey, TValue> configuration, ILogger<AsyncMemoryCache<TKey, TValue>> logger)
2334
where TKey : notnull
2435
where TValue : IAsyncDisposable
2536
{
26-
logger.LogTrace("Starting evictionbehavior - expiry check interval {Interval}.", _timer.Period);
37+
logger.LogTrace("Starting evictionbehavior - expiry check interval {Interval}.", _interval);
38+
#if NET8_0_OR_GREATER
2739
_workerTask = Task.Factory.StartNew(async () =>
2840
{
2941
try
@@ -37,10 +49,14 @@ public void Start<TKey, TValue>(IAsyncMemoryCacheConfiguration<TKey, TValue> con
3749
{
3850
logger.LogTrace("CancellationToken was cancelled.");
3951
}
52+
53+
logger.LogTrace("Stopping behavior.");
4054
}, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default)
4155
.Unwrap();
4256

43-
logger.LogTrace("Stopping behavior.");
57+
#else
58+
_timer = new(async _ => await CheckExpiredItems(configuration, logger).ConfigureAwait(false), null, 0, (uint)_interval.TotalMilliseconds);
59+
#endif
4460
}
4561

4662
private static async Task CheckExpiredItems<TKey, TValue>(IAsyncMemoryCacheConfiguration<TKey, TValue> configuration, ILogger<AsyncMemoryCache<TKey, TValue>> logger)
@@ -83,6 +99,7 @@ private static async Task CheckExpiredItems<TKey, TValue>(IAsyncMemoryCacheConfi
8399
logger.LogTrace("Done checking expired items. Evicted {EvictedItemsCount} items.", expiredItems.Count);
84100
}
85101

102+
#if NET8_0_OR_GREATER
86103
public async ValueTask DisposeAsync()
87104
{
88105
_timer.Dispose();
@@ -95,4 +112,14 @@ public async ValueTask DisposeAsync()
95112

96113
_cts.Dispose();
97114
}
115+
#else
116+
public ValueTask DisposeAsync()
117+
{
118+
_timer?.Dispose();
119+
_cts.Cancel();
120+
_cts.Dispose();
121+
122+
return default;
123+
}
124+
#endif
98125
}

AsyncMemoryCache/EvictionBehaviors/EvictionBehavior.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ namespace AsyncMemoryCache.EvictionBehaviors;
55

66
public static class EvictionBehavior
77
{
8+
#if NET8_0_OR_GREATER
89
public static readonly IEvictionBehavior Default = new DefaultEvictionBehavior(TimeProvider.System);
10+
#else
11+
public static readonly IEvictionBehavior Default = new DefaultEvictionBehavior();
12+
#endif
913
public static readonly IEvictionBehavior Disabled = new NoOpEvictionBehavior();
1014
}
1115

AsyncMemoryCache/EvictionBehaviors/NoOpEvictionBehavior.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
namespace AsyncMemoryCache.EvictionBehaviors;
77

8+
#if NET8_0_OR_GREATER
89
[ExcludeFromCodeCoverage(Justification = "Nothing to test")]
10+
#endif
911
internal sealed class NoOpEvictionBehavior : IEvictionBehavior
1012
{
1113
public void Start<TKey, TValue>(IAsyncMemoryCacheConfiguration<TKey, TValue> configuration, ILogger<AsyncMemoryCache<TKey, TValue>>? logger)
@@ -14,5 +16,9 @@ public void Start<TKey, TValue>(IAsyncMemoryCacheConfiguration<TKey, TValue> con
1416
{
1517
}
1618

19+
#if NET8_0_OR_GREATER
1720
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
21+
#else
22+
public ValueTask DisposeAsync() => default;
23+
#endif
1824
}

AsyncMemoryCache/ExpirationStrategy/AbsoluteExpirationStrategy.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ internal AbsoluteExpirationStrategy(DateTimeOffset expiryDate)
1515
public bool IsExpired()
1616
=> DateTimeOffset.UtcNow > AbsoluteExpiration;
1717

18+
#if NET8_0_OR_GREATER
1819
[ExcludeFromCodeCoverage(Justification = "Empty implementation")]
20+
#endif
1921
public void CacheEntityAccessed()
2022
{
2123
}

0 commit comments

Comments
 (0)