From 444ecddd963e035f0eaddaa710cfd745093bce29 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 8 Apr 2024 20:14:58 +1000 Subject: [PATCH 1/2] elide some asyncs --- .../ExecutionBenchmarkAsync.cs | 6 +++--- .../Distributed/DistributedCacheAccessor_Async.cs | 4 ++-- .../DistributedCacheLevelTests.cs | 10 +++++----- .../MemoryLevelTests.cs | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/benchmarks/ZiggyCreatures.FusionCache.Benchmarks/ExecutionBenchmarkAsync.cs b/benchmarks/ZiggyCreatures.FusionCache.Benchmarks/ExecutionBenchmarkAsync.cs index 9052ba5e..c70d3dc6 100644 --- a/benchmarks/ZiggyCreatures.FusionCache.Benchmarks/ExecutionBenchmarkAsync.cs +++ b/benchmarks/ZiggyCreatures.FusionCache.Benchmarks/ExecutionBenchmarkAsync.cs @@ -34,7 +34,7 @@ private async Task ExecutorAsync() public async Task WithTimeout() { await RunUtils.RunAsyncActionAdvancedAsync( - async _ => await ExecutorAsync(), + _ => ExecutorAsync(), TimeSpan.FromSeconds(2), false, true @@ -45,7 +45,7 @@ await RunUtils.RunAsyncActionAdvancedAsync( public async Task WithTimeout2() { await RunUtils.RunAsyncActionAdvancedAsync( - async _ => await ExecutorAsync(), + _ => ExecutorAsync(), TimeSpan.FromSeconds(2), true, true @@ -56,7 +56,7 @@ await RunUtils.RunAsyncActionAdvancedAsync( public async Task WithoutTimeout() { await RunUtils.RunAsyncActionAdvancedAsync( - async _ => await ExecutorAsync(), + _ => ExecutorAsync(), Timeout.InfiniteTimeSpan, true, true diff --git a/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs b/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs index 020be3dc..14f76ef3 100644 --- a/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs +++ b/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs @@ -148,8 +148,8 @@ public async ValueTask SetEntryAsync(string operationId, string ke try { timeout ??= options.GetAppropriateDistributedCacheTimeout(hasFallbackValue); - data = await RunUtils.RunAsyncFuncWithTimeoutAsync( - async ct => await _cache.GetAsync(MaybeProcessCacheKey(key), ct).ConfigureAwait(false), + data = await RunUtils.RunAsyncFuncWithTimeoutAsync( + ct => _cache.GetAsync(MaybeProcessCacheKey(key), ct), timeout.Value, true, token: token diff --git a/tests/ZiggyCreatures.FusionCache.Tests/DistributedCacheLevelTests.cs b/tests/ZiggyCreatures.FusionCache.Tests/DistributedCacheLevelTests.cs index e7587c56..9964744e 100644 --- a/tests/ZiggyCreatures.FusionCache.Tests/DistributedCacheLevelTests.cs +++ b/tests/ZiggyCreatures.FusionCache.Tests/DistributedCacheLevelTests.cs @@ -969,22 +969,22 @@ static async Task FakeGetAsync(FusionCacheFactoryExecutionContext ctx, using var cache = new FusionCache(CreateFusionCacheOptions()).SetupDistributedCache(distributedCache, TestsUtils.GetSerializer(serializerType)); // TOT REQ + 1 / FULL RESP + 1 - var v1 = await cache.GetOrSetAsync(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v1 = await cache.GetOrSetAsync(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // CACHED -> NO INCR - var v2 = await cache.GetOrSetAsync(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v2 = await cache.GetOrSetAsync(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // LET THE CACHE EXPIRE await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1 - var v3 = await cache.GetOrSetAsync(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v3 = await cache.GetOrSetAsync(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // LET THE CACHE EXPIRE await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1 - var v4 = await cache.GetOrSetAsync(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v4 = await cache.GetOrSetAsync(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // SET VALUE -> CHANGE LAST MODIFIED endpoint.SetValue(42); @@ -993,7 +993,7 @@ static async Task FakeGetAsync(FusionCacheFactoryExecutionContext ctx, await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / FULL RESP + 1 - var v5 = await cache.GetOrSetAsync(keyFoo, async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v5 = await cache.GetOrSetAsync(keyFoo, (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); Assert.Equal(4, endpoint.TotalRequestsCount); Assert.Equal(3, endpoint.ConditionalRequestsCount); diff --git a/tests/ZiggyCreatures.FusionCache.Tests/MemoryLevelTests.cs b/tests/ZiggyCreatures.FusionCache.Tests/MemoryLevelTests.cs index 41c95b68..1a837515 100644 --- a/tests/ZiggyCreatures.FusionCache.Tests/MemoryLevelTests.cs +++ b/tests/ZiggyCreatures.FusionCache.Tests/MemoryLevelTests.cs @@ -1007,22 +1007,22 @@ static async Task FakeGetAsync(FusionCacheFactoryExecutionContext ctx, using var cache = new FusionCache(new FusionCacheOptions()); // TOT REQ + 1 / FULL RESP + 1 - var v1 = await cache.GetOrSetAsync("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v1 = await cache.GetOrSetAsync("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // CACHED -> NO INCR - var v2 = await cache.GetOrSetAsync("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v2 = await cache.GetOrSetAsync("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // LET THE CACHE EXPIRE await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1 - var v3 = await cache.GetOrSetAsync("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v3 = await cache.GetOrSetAsync("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // LET THE CACHE EXPIRE await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / NOT MOD RESP + 1 - var v4 = await cache.GetOrSetAsync("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v4 = await cache.GetOrSetAsync("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); // SET VALUE -> CHANGE LAST MODIFIED endpoint.SetValue(42); @@ -1031,7 +1031,7 @@ static async Task FakeGetAsync(FusionCacheFactoryExecutionContext ctx, await Task.Delay(duration.PlusALittleBit()); // TOT REQ + 1 / COND REQ + 1 / FULL RESP + 1 - var v5 = await cache.GetOrSetAsync("foo", async (ctx, _) => await FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); + var v5 = await cache.GetOrSetAsync("foo", (ctx, _) => FakeGetAsync(ctx, endpoint), opt => opt.SetDuration(duration).SetFailSafe(true)); Assert.Equal(4, endpoint.TotalRequestsCount); Assert.Equal(3, endpoint.ConditionalRequestsCount); From 5e82342f88fd928b388e7a44b452125025e08f64 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 8 Apr 2024 20:17:39 +1000 Subject: [PATCH 2/2] . --- src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs | 4 ++-- .../Distributed/DistributedCacheAccessor_Async.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs b/src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs index 4094df93..4305a123 100644 --- a/src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs +++ b/src/ZiggyCreatures.FusionCache.Chaos/ChaosMemoryLocker.cs @@ -35,10 +35,10 @@ public ChaosMemoryLocker(IFusionCacheMemoryLocker innerMemoryLocker, ILogger - public async ValueTask AcquireLockAsync(string cacheName, string cacheInstanceId, string operationId, string key, TimeSpan timeout, ILogger? logger, CancellationToken token) + public ValueTask AcquireLockAsync(string cacheName, string cacheInstanceId, string operationId, string key, TimeSpan timeout, ILogger? logger, CancellationToken token) { MaybeChaos(token); - return await _innerMemoryLocker.AcquireLockAsync(cacheName, cacheInstanceId, operationId, key, timeout, logger, token); + return _innerMemoryLocker.AcquireLockAsync(cacheName, cacheInstanceId, operationId, key, timeout, logger, token); } /// diff --git a/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs b/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs index 14f76ef3..cac43982 100644 --- a/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs +++ b/src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs @@ -255,15 +255,15 @@ public async ValueTask SetEntryAsync(string operationId, string ke return (null, false); } - public async ValueTask RemoveEntryAsync(string operationId, string key, FusionCacheEntryOptions options, bool isBackground, CancellationToken token) + public ValueTask RemoveEntryAsync(string operationId, string key, FusionCacheEntryOptions options, bool isBackground, CancellationToken token) { if (IsCurrentlyUsable(operationId, key) == false) - return false; + return new ValueTask(false); // ACTIVITY using var activity = Activities.SourceDistributedLevel.StartActivityWithCommonTags(Activities.Names.DistributedRemove, _options.CacheName, _options.InstanceId!, key, operationId, CacheLevelKind.Distributed); - return await ExecuteOperationAsync( + return ExecuteOperationAsync( operationId, key, async ct => @@ -276,6 +276,6 @@ public async ValueTask RemoveEntryAsync(string operationId, string key, Fu "removing entry from distributed" + isBackground.ToString(" (background)"), options, token - ).ConfigureAwait(false); + ); } }