diff --git a/src/CacheTower.Extensions.Redis/RedisLockExtension.cs b/src/CacheTower.Extensions.Redis/RedisLockExtension.cs index 27ca21d..9f49455 100644 --- a/src/CacheTower.Extensions.Redis/RedisLockExtension.cs +++ b/src/CacheTower.Extensions.Redis/RedisLockExtension.cs @@ -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); } @@ -86,7 +86,7 @@ private async ValueTask SpinWaitAsync(TaskCompletionSource 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; } @@ -107,7 +107,7 @@ private async ValueTask DelayWaitAsync(TaskCompletionSource taskCompletion public async ValueTask 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) { @@ -132,13 +132,13 @@ public async ValueTask 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); } } diff --git a/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs b/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs index 7c9d87b..6412ae7 100644 --- a/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs +++ b/src/CacheTower.Extensions.Redis/RedisRemoteEvictionExtension.cs @@ -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); } /// @@ -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); } /// @@ -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); } } } @@ -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); } } } diff --git a/src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs b/src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs index 7473dd7..c5e9664 100644 --- a/src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs +++ b/src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs @@ -46,34 +46,34 @@ private async ValueTask TryConfigureIndexes() if (!HasSetIndexes) { HasSetIndexes = true; - await EntityIndexWriter.ApplyIndexingAsync(Connection); + await EntityIndexWriter.ApplyIndexingAsync(Connection).ConfigureAwait(false); } } /// public async ValueTask CleanupAsync() { - await TryConfigureIndexes(); - await EntityCommandWriter.WriteAsync(Connection, new[] { new CleanupCommand() }, default); + await TryConfigureIndexes().ConfigureAwait(false); + await EntityCommandWriter.WriteAsync(Connection, new[] { new CleanupCommand() }, default).ConfigureAwait(false); } /// public async ValueTask EvictAsync(string cacheKey) { - await TryConfigureIndexes(); - await EntityCommandWriter.WriteAsync(Connection, new[] { new EvictCommand(cacheKey) }, default); + await TryConfigureIndexes().ConfigureAwait(false); + await EntityCommandWriter.WriteAsync(Connection, new[] { new EvictCommand(cacheKey) }, default).ConfigureAwait(false); } /// public async ValueTask FlushAsync() { - await EntityCommandWriter.WriteAsync(Connection, new[] { new FlushCommand() }, default); + await EntityCommandWriter.WriteAsync(Connection, new[] { new FlushCommand() }, default).ConfigureAwait(false); } /// public async ValueTask?> GetAsync(string cacheKey) { - await TryConfigureIndexes(); + await TryConfigureIndexes().ConfigureAwait(false); var provider = new MongoFrameworkQueryProvider(Connection); var queryable = new MongoFrameworkQueryable(provider); @@ -92,7 +92,7 @@ public async ValueTask FlushAsync() /// public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) { - await TryConfigureIndexes(); + await TryConfigureIndexes().ConfigureAwait(false); var command = new SetCommand(new DbCachedEntry { CacheKey = cacheKey, @@ -100,7 +100,7 @@ public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) Value = cacheEntry.Value! }); - await EntityCommandWriter.WriteAsync(Connection, new[] { command }, default); + await EntityCommandWriter.WriteAsync(Connection, new[] { command }, default).ConfigureAwait(false); } /// @@ -110,7 +110,7 @@ public async ValueTask IsAvailableAsync(string cacheKey) { try { - await TryConfigureIndexes(); + await TryConfigureIndexes().ConfigureAwait(false); IsDatabaseAvailable = true; } catch diff --git a/src/CacheTower.Providers.Redis/RedisCacheLayer.cs b/src/CacheTower.Providers.Redis/RedisCacheLayer.cs index 7fb03e5..f093631 100644 --- a/src/CacheTower.Providers.Redis/RedisCacheLayer.cs +++ b/src/CacheTower.Providers.Redis/RedisCacheLayer.cs @@ -52,7 +52,7 @@ public ValueTask CleanupAsync() /// public async ValueTask EvictAsync(string cacheKey) { - await Database.KeyDeleteAsync(cacheKey); + await Database.KeyDeleteAsync(cacheKey).ConfigureAwait(false); } /// @@ -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); } } /// public async ValueTask?> GetAsync(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); @@ -101,7 +101,7 @@ public async ValueTask SetAsync(string cacheKey, CacheEntry 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); } } } diff --git a/src/CacheTower/CacheStack.cs b/src/CacheTower/CacheStack.cs index 62a68e0..495ca26 100644 --- a/src/CacheTower/CacheStack.cs +++ b/src/CacheTower/CacheStack.cs @@ -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); } /// @@ -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); } } @@ -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); } /// @@ -154,7 +154,7 @@ public async ValueTask> SetAsync(string cacheKey, T value, Time } var cacheEntry = new CacheEntry(value, timeToLive); - await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry); + await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry).ConfigureAwait(false); return cacheEntry; } @@ -173,7 +173,7 @@ public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) throw new ArgumentNullException(nameof(cacheEntry)); } - await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry); + await InternalSetAsync(cacheKey, cacheEntry, CacheUpdateType.AddOrUpdateEntry).ConfigureAwait(false); } private async ValueTask InternalSetAsync(string cacheKey, CacheEntry cacheEntry, CacheUpdateType cacheUpdateType) @@ -184,7 +184,7 @@ private async ValueTask InternalSetAsync(string cacheKey, CacheEntry cache try { - await layer.SetAsync(cacheKey, cacheEntry); + await layer.SetAsync(cacheKey, cacheEntry).ConfigureAwait(false); } catch (CacheSerializationException ex) { @@ -192,7 +192,7 @@ private async ValueTask InternalSetAsync(string cacheKey, CacheEntry cache } } - await Extensions.OnCacheUpdateAsync(cacheKey, cacheEntry.Expiry, cacheUpdateType); + await Extensions.OnCacheUpdateAsync(cacheKey, cacheEntry.Expiry, cacheUpdateType).ConfigureAwait(false); } /// @@ -208,11 +208,11 @@ private async ValueTask InternalSetAsync(string cacheKey, CacheEntry 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(cacheKey); + var cacheEntry = await layer.GetAsync(cacheKey).ConfigureAwait(false); if (cacheEntry != default) { return cacheEntry; @@ -234,11 +234,11 @@ private async ValueTask InternalSetAsync(string cacheKey, CacheEntry 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(cacheKey); + var cacheEntry = await layer.GetAsync(cacheKey).ConfigureAwait(false); if (cacheEntry != default) { return (layerIndex, cacheEntry); @@ -270,7 +270,7 @@ public async ValueTask GetOrSetAsync(string cacheKey, Func> val } var currentTime = DateTimeProvider.Now; - var cacheEntryPoint = await GetWithLayerIndexAsync(cacheKey); + var cacheEntryPoint = await GetWithLayerIndexAsync(cacheKey).ConfigureAwait(false); var cacheEntryStatus = CacheEntryStatus.Stale; if (cacheEntryPoint != default) { @@ -302,7 +302,7 @@ public async ValueTask GetOrSetAsync(string cacheKey, Func> 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(int fromIndexExclusive, string cacheKey, CacheEntry cacheEntry) @@ -314,9 +314,9 @@ private async ValueTask BackPopulateCacheAsync(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); } } } @@ -333,7 +333,7 @@ private async ValueTask BackPopulateCacheAsync(int fromIndexExclusive, string { try { - var previousEntry = await GetAsync(cacheKey); + var previousEntry = await GetAsync(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. @@ -344,26 +344,26 @@ private async ValueTask BackPopulateCacheAsync(int fromIndexExclusive, string return previousEntry; } - await using var distributedLock = await Extensions.AwaitAccessAsync(cacheKey); + await using var distributedLock = await Extensions.AwaitAccessAsync(cacheKey).ConfigureAwait(false); CacheEntry? 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(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(cacheKey); + cacheEntry = await GetAsync(cacheKey).ConfigureAwait(false); } return cacheEntry; @@ -377,7 +377,7 @@ private async ValueTask BackPopulateCacheAsync(int fromIndexExclusive, string else if (entryStatus != CacheEntryStatus.Stale) { //Last minute check to confirm whether waiting is required - var currentEntry = await GetAsync(cacheKey); + var currentEntry = await GetAsync(cacheKey).ConfigureAwait(false); if (currentEntry != null && currentEntry.GetStaleDate(settings) > DateTimeProvider.Now) { KeyLock.ReleaseLock(cacheKey, currentEntry); @@ -385,7 +385,7 @@ private async ValueTask BackPopulateCacheAsync(int fromIndexExclusive, string } //Lock until we are notified to be unlocked - return await KeyLock.WaitAsync(cacheKey) as CacheEntry; + return await KeyLock.WaitAsync(cacheKey).ConfigureAwait(false) as CacheEntry; } return default; @@ -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; } diff --git a/src/CacheTower/CacheStack{TContext}.cs b/src/CacheTower/CacheStack{TContext}.cs index 3ccfd45..25bbc7c 100644 --- a/src/CacheTower/CacheStack{TContext}.cs +++ b/src/CacheTower/CacheStack{TContext}.cs @@ -57,7 +57,7 @@ public async ValueTask GetOrSetAsync(string cacheKey, Func(memStream); } @@ -72,7 +72,7 @@ private async Task SerializeFileAsync(string path, T value) using var memStream = new MemoryStream(); Options.Serializer.Serialize(memStream, value); memStream.Seek(0, SeekOrigin.Begin); - await memStream.CopyToAsync(stream); + await memStream.CopyToAsync(stream).ConfigureAwait(false); } private async Task TryLoadManifestAsync() @@ -80,7 +80,7 @@ private async Task TryLoadManifestAsync() //Avoid unnecessary lock contention way after manifest is loaded by checking before lock if (CacheManifest is null) { - await ManifestLock.WaitAsync(); + await ManifestLock.WaitAsync().ConfigureAwait(false); try { //Check that once we have lock (due to a race condition on the outer check) that we still need to load the manifest @@ -88,7 +88,7 @@ private async Task TryLoadManifestAsync() { if (File.Exists(ManifestPath)) { - CacheManifest = await DeserializeFileAsync>(ManifestPath); + CacheManifest = await DeserializeFileAsync>(ManifestPath).ConfigureAwait(false); CacheManifest ??= new(); } else @@ -99,7 +99,7 @@ private async Task TryLoadManifestAsync() } CacheManifest = new(); - await SerializeFileAsync(ManifestPath, CacheManifest); + await SerializeFileAsync(ManifestPath, CacheManifest).ConfigureAwait(false); } } } @@ -116,7 +116,7 @@ private async Task TryLoadManifestAsync() /// public async Task SaveManifestAsync() { - await ManifestLock.WaitAsync(); + await ManifestLock.WaitAsync().ConfigureAwait(false); try { if (!Directory.Exists(Options.DirectoryPath)) @@ -124,7 +124,7 @@ public async Task SaveManifestAsync() Directory.CreateDirectory(Options.DirectoryPath); } - await SerializeFileAsync(ManifestPath, CacheManifest); + await SerializeFileAsync(ManifestPath, CacheManifest).ConfigureAwait(false); } finally { @@ -135,7 +135,7 @@ public async Task SaveManifestAsync() /// public async ValueTask CleanupAsync() { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); var currentTime = DateTimeProvider.Now; foreach (var cachePair in CacheManifest!) @@ -161,7 +161,7 @@ public async ValueTask CleanupAsync() /// public async ValueTask EvictAsync(string cacheKey) { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); if (CacheManifest!.TryRemove(cacheKey, out var manifestEntry)) { @@ -182,7 +182,7 @@ public async ValueTask EvictAsync(string cacheKey) /// public async ValueTask FlushAsync() { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); foreach (var manifestEntry in CacheManifest!.Values) { @@ -201,13 +201,13 @@ public async ValueTask FlushAsync() CacheManifest.Clear(); - await SaveManifestAsync(); + await SaveManifestAsync().ConfigureAwait(false); } /// public async ValueTask?> GetAsync(string cacheKey) { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); if (CacheManifest!.TryGetValue(cacheKey, out var manifestEntry)) { @@ -220,7 +220,7 @@ public async ValueTask FlushAsync() var path = Path.Combine(Options.DirectoryPath, manifestEntry.FileName); if (File.Exists(path)) { - var value = await DeserializeFileAsync(path); + var value = await DeserializeFileAsync(path).ConfigureAwait(false); return new CacheEntry(value, manifestEntry.Expiry); } @@ -247,7 +247,7 @@ public async ValueTask IsAvailableAsync(string cacheKey) { try { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); IsManifestAvailable = true; } catch @@ -262,7 +262,7 @@ public async ValueTask IsAvailableAsync(string cacheKey) /// public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) { - await TryLoadManifestAsync(); + await TryLoadManifestAsync().ConfigureAwait(false); //Update the manifest entry with the new expiry if (CacheManifest!.TryGetValue(cacheKey, out var manifestEntry)) @@ -286,7 +286,7 @@ public async ValueTask SetAsync(string cacheKey, CacheEntry cacheEntry) using (await lockObj.WriterLockAsync()) { var path = Path.Combine(Options.DirectoryPath, manifestEntry.FileName); - await SerializeFileAsync(path, cacheEntry.Value); + await SerializeFileAsync(path, cacheEntry.Value).ConfigureAwait(false); } } @@ -304,9 +304,9 @@ public async ValueTask DisposeAsync() ManifestSavingCancellationTokenSource.Cancel(); if (!BackgroundManifestSavingTask.IsFaulted) { - await BackgroundManifestSavingTask; + await BackgroundManifestSavingTask.ConfigureAwait(false); } - await SaveManifestAsync(); + await SaveManifestAsync().ConfigureAwait(false); ManifestLock.Dispose();