Skip to content
Merged
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
36 changes: 15 additions & 21 deletions src/CacheTower/CacheStack.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using CacheTower.Extensions;
Expand All @@ -15,7 +14,7 @@ public class CacheStack : ICacheStack, IFlushableCacheStack, IAsyncDisposable
{
private bool Disposed;

private Dictionary<string, IEnumerable<TaskCompletionSource<object>>> WaitingKeyRefresh { get; }
private Dictionary<string, TaskCompletionSource<CacheEntry>> WaitingKeyRefresh { get; }

private ICacheLayer[] CacheLayers { get; }

Expand All @@ -38,7 +37,7 @@ public CacheStack(ICacheLayer[] cacheLayers, ICacheExtension[] extensions)
Extensions = new ExtensionContainer(extensions);
Extensions.Register(this);

WaitingKeyRefresh = new Dictionary<string, IEnumerable<TaskCompletionSource<object>>>(StringComparer.Ordinal);
WaitingKeyRefresh = new Dictionary<string, TaskCompletionSource<CacheEntry>>(StringComparer.Ordinal);
}

/// <summary>
Expand Down Expand Up @@ -229,10 +228,10 @@ private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string
hasLock = !WaitingKeyRefresh.ContainsKey(cacheKey);
if (hasLock)
{
WaitingKeyRefresh[cacheKey] = Array.Empty<TaskCompletionSource<object>>();
WaitingKeyRefresh[cacheKey] = null;
}
#elif NETSTANDARD2_1
hasLock = WaitingKeyRefresh.TryAdd(cacheKey, Array.Empty<TaskCompletionSource<object>>());
hasLock = WaitingKeyRefresh.TryAdd(cacheKey, null);
#endif
}

Expand Down Expand Up @@ -267,10 +266,10 @@ private async ValueTask<CacheEntry<T>> RefreshValueAsync<T>(string cacheKey, Fun
hasLock = !WaitingKeyRefresh.ContainsKey(cacheKey);
if (hasLock)
{
WaitingKeyRefresh[cacheKey] = Array.Empty<TaskCompletionSource<object>>();
WaitingKeyRefresh[cacheKey] = null;
}
#elif NETSTANDARD2_1
hasLock = WaitingKeyRefresh.TryAdd(cacheKey, Array.Empty<TaskCompletionSource<object>>());
hasLock = WaitingKeyRefresh.TryAdd(cacheKey, null);
#endif
}

Expand Down Expand Up @@ -313,18 +312,18 @@ private async ValueTask<CacheEntry<T>> RefreshValueAsync<T>(string cacheKey, Fun
}
else if (noExistingValueAvailable)
{
var delayedResultSource = new TaskCompletionSource<object>();
TaskCompletionSource<CacheEntry> completionSource;

lock (WaitingKeyRefresh)
{
var waitList = new[] { delayedResultSource };
if (WaitingKeyRefresh.TryGetValue(cacheKey, out var oldList))
if (!WaitingKeyRefresh.TryGetValue(cacheKey, out completionSource))
{
WaitingKeyRefresh[cacheKey] = oldList.Concat(waitList);
WaitingKeyRefresh[cacheKey] = new TaskCompletionSource<CacheEntry>();
}
else
else if (completionSource == null)
{
WaitingKeyRefresh[cacheKey] = waitList;
completionSource = new TaskCompletionSource<CacheEntry>();
WaitingKeyRefresh[cacheKey] = completionSource;
}
}

Expand All @@ -337,7 +336,7 @@ private async ValueTask<CacheEntry<T>> RefreshValueAsync<T>(string cacheKey, Fun
}

//Lock until we are notified to be unlocked
var result = await delayedResultSource.Task;
var result = await completionSource.Task;
return result as CacheEntry<T>;
}

Expand All @@ -348,15 +347,10 @@ private void UnlockWaitingTasks(string cacheKey, CacheEntry cacheEntry)
{
lock (WaitingKeyRefresh)
{
if (WaitingKeyRefresh.TryGetValue(cacheKey, out var waitingTasks))
if (WaitingKeyRefresh.TryGetValue(cacheKey, out var completionSource))
{
WaitingKeyRefresh.Remove(cacheKey);

var tasks = waitingTasks.ToArray();
for (int i = 0, l = tasks.Length; i < l; i++)
{
tasks[i].TrySetResult(cacheEntry);
}
completionSource?.TrySetResult(cacheEntry);
}
}
}
Expand Down