Skip to content
Open
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
8 changes: 5 additions & 3 deletions PersistentQueue/Cache/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Persistent.Queue.Cache;
public class Cache<TKey, TValue> : ICache<TKey, TValue> where TKey : notnull
{
private readonly CancellationTokenSource _cts = new();
private readonly CancellationToken _cancellationToken;
private readonly Dictionary<TKey, CacheItem> _items = new();
private readonly ReaderWriterLockSlim _lock = new();
private readonly TimeSpan _ttl;
Expand All @@ -17,7 +18,8 @@ public Cache(TimeSpan ttl)
else
_ttl = TimeSpan.FromSeconds(1);

Task.Factory.StartNew(CleanupLoop, _cts.Token);
_cancellationToken = _cts.Token;
Task.Factory.StartNew(CleanupLoop, _cancellationToken);
}

public TValue GetOrCreate(TKey key, Func<TValue> factory)
Expand Down Expand Up @@ -142,10 +144,10 @@ private async void CleanupLoop()
{
try
{
while (!_cts.IsCancellationRequested)
while (!_cancellationToken.IsCancellationRequested)
{
RemoveOldItems();
await Task.Delay(_ttl / 2, _cts.Token);
await Task.Delay(_ttl / 2, _cancellationToken);
}
}
catch (OperationCanceledException)
Expand Down