-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add FusionCache comparison * Refactor
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using LazyCache; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using ZiggyCreatures.Caching.Fusion; | ||
|
||
namespace FastCache.Benchmarks; | ||
|
||
[ShortRunJob] | ||
[MemoryDiagnoser] | ||
[DisassemblyDiagnoser(maxDepth: 5, exportCombinedDisassemblyReport: true)] | ||
public class ComparisonAsync | ||
{ | ||
[Params("A", "abcd", "long ass string with букви кирилицею AND UPPERCASE")] | ||
public string ItemKey = default!; | ||
public string ItemValue = "item value"; | ||
|
||
private readonly MemoryCache _memoryCache = new(new MemoryCacheOptions()); | ||
|
||
private readonly IAppCache _lazyCache = new CachingService(); | ||
private readonly FusionCache _fusionCache = new (new FusionCacheOptions()); | ||
|
||
private readonly TimeSpan _expire = TimeSpan.FromMinutes(60); | ||
|
||
[GlobalSetup] | ||
public void Initialize() | ||
{ | ||
Services.CacheManager.SuspendEviction<string, string>(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public async Task<string> GetOrComputeCached() | ||
{ | ||
return await Cached.GetOrCompute(ItemKey, GetString, _expire); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<string?> GetOrComputeMemoryCache() | ||
{ | ||
return await _memoryCache.GetOrCreateAsync(ItemKey, async factory => | ||
{ | ||
factory.SetAbsoluteExpiration(_expire); | ||
return await GetString(ItemKey); | ||
}) ?? Unreachable<string>(); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<string> GetOrComputeLazyCache() | ||
{ | ||
return await _lazyCache.GetOrAddAsync<string>(ItemKey, async factory => | ||
{ | ||
factory.SetAbsoluteExpiration(_expire); | ||
return await GetString(ItemKey); | ||
}); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<string> GetOrComputeFusionCache() | ||
{ | ||
return await _fusionCache.GetOrSetAsync<string>(ItemKey, async (context, _) => | ||
{ | ||
context.Options.SetDuration(_expire); | ||
return await GetString(ItemKey); | ||
}) ?? Unreachable<string>(); | ||
} | ||
|
||
private async Task<string> GetString(string key) | ||
{ | ||
await Task.Delay(10); | ||
return await Task.FromResult(ItemValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters