Skip to content

Commit

Permalink
Add .ConfigureAwait(false) to awaited calls (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
wazzamatazz authored Jul 31, 2024
1 parent 25b4f6d commit 87de169
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/CacheTower.Extensions.Redis/RedisLockExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public void Register(ICacheStack cacheStack)
private async ValueTask ReleaseLockAsync(string cacheKey)
{
var lockKey = string.Format(Options.KeyFormat, cacheKey);
await Subscriber.PublishAsync(GetRedisChannel(), cacheKey, CommandFlags.FireAndForget);
await Database.KeyDeleteAsync(lockKey, CommandFlags.FireAndForget);
await Subscriber.PublishAsync(GetRedisChannel(), cacheKey, CommandFlags.FireAndForget).ConfigureAwait(false);
await Database.KeyDeleteAsync(lockKey, CommandFlags.FireAndForget).ConfigureAwait(false);
UnlockWaitingTasks(cacheKey);
}

Expand All @@ -86,7 +86,7 @@ private async ValueTask SpinWaitAsync(TaskCompletionSource<bool> taskCompletionS
var lockExists = await Database.KeyExistsAsync(lockKey).ConfigureAwait(false);
if (lockExists)
{
await Task.Delay(Options.LockCheckStrategy.SpinTime);
await Task.Delay(Options.LockCheckStrategy.SpinTime).ConfigureAwait(false);
continue;
}

Expand All @@ -107,7 +107,7 @@ private async ValueTask DelayWaitAsync(TaskCompletionSource<bool> taskCompletion
public async ValueTask<DistributedLock> AwaitAccessAsync(string cacheKey)
{
var lockKey = string.Format(Options.KeyFormat, cacheKey);
var hasLock = await Database.StringSetAsync(lockKey, RedisValue.EmptyString, expiry: Options.LockTimeout, when: When.NotExists);
var hasLock = await Database.StringSetAsync(lockKey, RedisValue.EmptyString, expiry: Options.LockTimeout, when: When.NotExists).ConfigureAwait(false);

if (hasLock)
{
Expand All @@ -132,13 +132,13 @@ public async ValueTask<DistributedLock> AwaitAccessAsync(string cacheKey)
});

//Last minute check to confirm whether waiting is required (in case the notification is missed)
if (!await Database.KeyExistsAsync(lockKey))
if (!await Database.KeyExistsAsync(lockKey).ConfigureAwait(false))
{
UnlockWaitingTasks(cacheKey);
return DistributedLock.Unlocked(cacheKey);
}

await completionSource.Task;
await completionSource.Task.ConfigureAwait(false);
return DistributedLock.Unlocked(cacheKey);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private async ValueTask FlagEvictionAsync(string cacheKey)
FlaggedEvictions.Add(cacheKey);
}

await Subscriber.PublishAsync(EvictionChannel, cacheKey, CommandFlags.FireAndForget);
await Subscriber.PublishAsync(EvictionChannel, cacheKey, CommandFlags.FireAndForget).ConfigureAwait(false);
}

/// <remarks>
Expand All @@ -86,7 +86,7 @@ public async ValueTask OnCacheFlushAsync()
HasFlushTriggered = true;
}

await Subscriber.PublishAsync(FlushChannel, RedisValue.EmptyString, CommandFlags.FireAndForget);
await Subscriber.PublishAsync(FlushChannel, RedisValue.EmptyString, CommandFlags.FireAndForget).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -122,7 +122,7 @@ public void Register(ICacheStack cacheStack)
var cacheLayer = cacheLayers[i];
if (cacheLayer is ILocalCacheLayer)
{
await cacheLayer.EvictAsync(cacheKey);
await cacheLayer.EvictAsync(cacheKey).ConfigureAwait(false);
}
}
}
Expand All @@ -146,7 +146,7 @@ public void Register(ICacheStack cacheStack)
var cacheLayer = cacheLayers[i];
if (cacheLayer is ILocalCacheLayer)
{
await cacheLayer.FlushAsync();
await cacheLayer.FlushAsync().ConfigureAwait(false);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,34 @@ private async ValueTask TryConfigureIndexes()
if (!HasSetIndexes)
{
HasSetIndexes = true;
await EntityIndexWriter.ApplyIndexingAsync<DbCachedEntry>(Connection);
await EntityIndexWriter.ApplyIndexingAsync<DbCachedEntry>(Connection).ConfigureAwait(false);
}
}

/// <inheritdoc/>
public async ValueTask CleanupAsync()
{
await TryConfigureIndexes();
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new CleanupCommand() }, default);
await TryConfigureIndexes().ConfigureAwait(false);
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new CleanupCommand() }, default).ConfigureAwait(false);
}

/// <inheritdoc/>
public async ValueTask EvictAsync(string cacheKey)
{
await TryConfigureIndexes();
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new EvictCommand(cacheKey) }, default);
await TryConfigureIndexes().ConfigureAwait(false);
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new EvictCommand(cacheKey) }, default).ConfigureAwait(false);
}

/// <inheritdoc/>
public async ValueTask FlushAsync()
{
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new FlushCommand() }, default);
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new FlushCommand() }, default).ConfigureAwait(false);
}

/// <inheritdoc/>
public async ValueTask<CacheEntry<T>?> GetAsync<T>(string cacheKey)
{
await TryConfigureIndexes();
await TryConfigureIndexes().ConfigureAwait(false);

var provider = new MongoFrameworkQueryProvider<DbCachedEntry>(Connection);
var queryable = new MongoFrameworkQueryable<DbCachedEntry>(provider);
Expand All @@ -92,15 +92,15 @@ public async ValueTask FlushAsync()
/// <inheritdoc/>
public async ValueTask SetAsync<T>(string cacheKey, CacheEntry<T> cacheEntry)
{
await TryConfigureIndexes();
await TryConfigureIndexes().ConfigureAwait(false);
var command = new SetCommand(new DbCachedEntry
{
CacheKey = cacheKey,
Expiry = cacheEntry.Expiry,
Value = cacheEntry.Value!
});

await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { command }, default);
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { command }, default).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -110,7 +110,7 @@ public async ValueTask<bool> IsAvailableAsync(string cacheKey)
{
try
{
await TryConfigureIndexes();
await TryConfigureIndexes().ConfigureAwait(false);
IsDatabaseAvailable = true;
}
catch
Expand Down
8 changes: 4 additions & 4 deletions src/CacheTower.Providers.Redis/RedisCacheLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ValueTask CleanupAsync()
/// <inheritdoc/>
public async ValueTask EvictAsync(string cacheKey)
{
await Database.KeyDeleteAsync(cacheKey);
await Database.KeyDeleteAsync(cacheKey).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -65,14 +65,14 @@ public async ValueTask FlushAsync()
var redisEndpoints = Connection.GetEndPoints();
foreach (var endpoint in redisEndpoints)
{
await Connection.GetServer(endpoint).FlushDatabaseAsync(Options.DatabaseIndex);
await Connection.GetServer(endpoint).FlushDatabaseAsync(Options.DatabaseIndex).ConfigureAwait(false);
}
}

/// <inheritdoc/>
public async ValueTask<CacheEntry<T>?> GetAsync<T>(string cacheKey)
{
var redisValue = await Database.StringGetAsync(cacheKey);
var redisValue = await Database.StringGetAsync(cacheKey).ConfigureAwait(false);
if (redisValue != RedisValue.Null)
{
using var stream = new MemoryStream(redisValue);
Expand Down Expand Up @@ -101,7 +101,7 @@ public async ValueTask SetAsync<T>(string cacheKey, CacheEntry<T> cacheEntry)
Options.Serializer.Serialize(stream, cacheEntry);
stream.Seek(0, SeekOrigin.Begin);
var redisValue = RedisValue.CreateFrom(stream);
await Database.StringSetAsync(cacheKey, redisValue, expiryOffset);
await Database.StringSetAsync(cacheKey, redisValue, expiryOffset).ConfigureAwait(false);
}
}
}
52 changes: 26 additions & 26 deletions src/CacheTower/CacheStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public async ValueTask FlushAsync()
for (int i = 0, l = CacheLayers.Length; i < l; i++)
{
var layer = CacheLayers[i];
await layer.FlushAsync();
await layer.FlushAsync().ConfigureAwait(false);
}

await Extensions.OnCacheFlushAsync();
await Extensions.OnCacheFlushAsync().ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -120,7 +120,7 @@ public async ValueTask CleanupAsync()
for (int i = 0, l = CacheLayers.Length; i < l; i++)
{
var layer = CacheLayers[i];
await layer.CleanupAsync();
await layer.CleanupAsync().ConfigureAwait(false);
}
}

Expand All @@ -137,10 +137,10 @@ public async ValueTask EvictAsync(string cacheKey)
for (int i = 0, l = CacheLayers.Length; i < l; i++)
{
var layer = CacheLayers[i];
await layer.EvictAsync(cacheKey);
await layer.EvictAsync(cacheKey).ConfigureAwait(false);
}

await Extensions.OnCacheEvictionAsync(cacheKey);
await Extensions.OnCacheEvictionAsync(cacheKey).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -154,7 +154,7 @@ public async ValueTask<CacheEntry<T>> SetAsync<T>(string cacheKey, T value, Time
}

var cacheEntry = new CacheEntry<T>(value, timeToLive);
await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry);
await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry).ConfigureAwait(false);
return cacheEntry;
}

Expand All @@ -173,7 +173,7 @@ public async ValueTask SetAsync<T>(string cacheKey, CacheEntry<T> cacheEntry)
throw new ArgumentNullException(nameof(cacheEntry));
}

await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry);
await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry).ConfigureAwait(false);
}

private async ValueTask InternalSetAsync<T>(string cacheKey, CacheEntry<T> cacheEntry, CacheUpdateType cacheUpdateType)
Expand All @@ -184,15 +184,15 @@ private async ValueTask InternalSetAsync<T>(string cacheKey, CacheEntry<T> cache

try
{
await layer.SetAsync(cacheKey, cacheEntry);
await layer.SetAsync(cacheKey, cacheEntry).ConfigureAwait(false);
}
catch (CacheSerializationException ex)
{
Logger.LogWarning(ex, "Unable to set CacheKey={CacheKey} on CacheLayer={CacheLayer} due to an exception. This will result in cache misses on this layer.", cacheKey, layer.GetType());
}
}

await Extensions.OnCacheUpdateAsync(cacheKey, cacheEntry.Expiry, cacheUpdateType);
await Extensions.OnCacheUpdateAsync(cacheKey, cacheEntry.Expiry, cacheUpdateType).ConfigureAwait(false);
}

/// <inheritdoc/>
Expand All @@ -208,11 +208,11 @@ private async ValueTask InternalSetAsync<T>(string cacheKey, CacheEntry<T> cache
for (var layerIndex = 0; layerIndex < CacheLayers.Length; layerIndex++)
{
var layer = CacheLayers[layerIndex];
if (await layer.IsAvailableAsync(cacheKey))
if (await layer.IsAvailableAsync(cacheKey).ConfigureAwait(false))
{
try
{
var cacheEntry = await layer.GetAsync<T>(cacheKey);
var cacheEntry = await layer.GetAsync<T>(cacheKey).ConfigureAwait(false);
if (cacheEntry != default)
{
return cacheEntry;
Expand All @@ -234,11 +234,11 @@ private async ValueTask InternalSetAsync<T>(string cacheKey, CacheEntry<T> cache
for (var layerIndex = 0; layerIndex < CacheLayers.Length; layerIndex++)
{
var layer = CacheLayers[layerIndex];
if (await layer.IsAvailableAsync(cacheKey))
if (await layer.IsAvailableAsync(cacheKey).ConfigureAwait(false))
{
try
{
var cacheEntry = await layer.GetAsync<T>(cacheKey);
var cacheEntry = await layer.GetAsync<T>(cacheKey).ConfigureAwait(false);
if (cacheEntry != default)
{
return (layerIndex, cacheEntry);
Expand Down Expand Up @@ -270,7 +270,7 @@ public async ValueTask<T> GetOrSetAsync<T>(string cacheKey, Func<T, Task<T>> val
}

var currentTime = DateTimeProvider.Now;
var cacheEntryPoint = await GetWithLayerIndexAsync<T>(cacheKey);
var cacheEntryPoint = await GetWithLayerIndexAsync<T>(cacheKey).ConfigureAwait(false);
var cacheEntryStatus = CacheEntryStatus.Stale;
if (cacheEntryPoint != default)
{
Expand Down Expand Up @@ -302,7 +302,7 @@ public async ValueTask<T> GetOrSetAsync<T>(string cacheKey, Func<T, Task<T>> val
cacheEntryStatus = CacheEntryStatus.Miss;
}

return (await RefreshValueAsync(cacheKey, valueFactory, settings, cacheEntryStatus))!.Value!;
return (await RefreshValueAsync(cacheKey, valueFactory, settings, cacheEntryStatus).ConfigureAwait(false))!.Value!;
}

private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string cacheKey, CacheEntry<T> cacheEntry)
Expand All @@ -314,9 +314,9 @@ private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string
for (; --fromIndexExclusive >= 0;)
{
var previousLayer = CacheLayers[fromIndexExclusive];
if (await previousLayer.IsAvailableAsync(cacheKey))
if (await previousLayer.IsAvailableAsync(cacheKey).ConfigureAwait(false))
{
await previousLayer.SetAsync(cacheKey, cacheEntry);
await previousLayer.SetAsync(cacheKey, cacheEntry).ConfigureAwait(false);
}
}
}
Expand All @@ -333,7 +333,7 @@ private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string
{
try
{
var previousEntry = await GetAsync<T>(cacheKey);
var previousEntry = await GetAsync<T>(cacheKey).ConfigureAwait(false);
if (previousEntry != default && entryStatus == CacheEntryStatus.Miss && previousEntry.Expiry >= DateTimeProvider.Now)
{
//The Cache Stack will always return an unexpired value if one exists.
Expand All @@ -344,26 +344,26 @@ private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string
return previousEntry;
}

await using var distributedLock = await Extensions.AwaitAccessAsync(cacheKey);
await using var distributedLock = await Extensions.AwaitAccessAsync(cacheKey).ConfigureAwait(false);

CacheEntry<T>? cacheEntry;
if (distributedLock.IsLockOwner)
{
var oldValue = previousEntry != default ? previousEntry.Value : default;
var refreshedValue = await asyncValueFactory(oldValue!);
var refreshedValue = await asyncValueFactory(oldValue!).ConfigureAwait(false);
cacheEntry = new CacheEntry<T>(refreshedValue, settings.TimeToLive);
var cacheUpdateType = entryStatus switch
{
CacheEntryStatus.Miss => CacheUpdateType.AddEntry,
_ => CacheUpdateType.AddOrUpdateEntry
};
await InternalSetAsync(cacheKey, cacheEntry, cacheUpdateType);
await InternalSetAsync(cacheKey, cacheEntry, cacheUpdateType).ConfigureAwait(false);

KeyLock.ReleaseLock(cacheKey, cacheEntry);
}
else
{
cacheEntry = await GetAsync<T>(cacheKey);
cacheEntry = await GetAsync<T>(cacheKey).ConfigureAwait(false);
}

return cacheEntry;
Expand All @@ -377,15 +377,15 @@ private async ValueTask BackPopulateCacheAsync<T>(int fromIndexExclusive, string
else if (entryStatus != CacheEntryStatus.Stale)
{
//Last minute check to confirm whether waiting is required
var currentEntry = await GetAsync<T>(cacheKey);
var currentEntry = await GetAsync<T>(cacheKey).ConfigureAwait(false);
if (currentEntry != null && currentEntry.GetStaleDate(settings) > DateTimeProvider.Now)
{
KeyLock.ReleaseLock(cacheKey, currentEntry);
return currentEntry;
}

//Lock until we are notified to be unlocked
return await KeyLock.WaitAsync(cacheKey) as CacheEntry<T>;
return await KeyLock.WaitAsync(cacheKey).ConfigureAwait(false) as CacheEntry<T>;
}

return default;
Expand All @@ -410,11 +410,11 @@ public async ValueTask DisposeAsync()
}
else if (layer is IAsyncDisposable asyncDisposableLayer)
{
await asyncDisposableLayer.DisposeAsync();
await asyncDisposableLayer.DisposeAsync().ConfigureAwait(false);
}
}

await Extensions.DisposeAsync();
await Extensions.DisposeAsync().ConfigureAwait(false);

Disposed = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/CacheTower/CacheStack{TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async ValueTask<T> GetOrSetAsync<T>(string cacheKey, Func<T, TContext, Ta
{
using var scope = CacheContextActivator.BeginScope();
var context = (TContext)scope.Resolve(typeof(TContext));
return await getter(old, context);
}, settings);
return await getter(old, context).ConfigureAwait(false);
}, settings).ConfigureAwait(false);
}
}
Loading

0 comments on commit 87de169

Please sign in to comment.