@@ -8,22 +8,34 @@ namespace AsyncMemoryCache.EvictionBehaviors;
88
99public 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}
0 commit comments