Skip to content

Commit

Permalink
Add FusionCache to comparison (#69)
Browse files Browse the repository at this point in the history
* Add FusionCache comparison

* Refactor
  • Loading branch information
rstm-sf committed Oct 24, 2023
1 parent 6b97f21 commit 5b5e696
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/FastCache.Benchmarks/Comparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FastCache.Extensions;
using LazyCache;
using Microsoft.Extensions.Caching.Memory;
using ZiggyCreatures.Caching.Fusion;

namespace FastCache.Benchmarks;

Expand All @@ -24,6 +25,7 @@ public class Comparison
.WithExpiration(CacheManager.Core.ExpirationMode.Absolute, TimeSpan.FromMinutes(60)));

private readonly IAppCache _lazyCache = new CachingService();
private readonly FusionCache _fusionCache = new (new FusionCacheOptions());

[GlobalSetup]
public void Initialize()
Expand All @@ -32,6 +34,7 @@ public void Initialize()
_memoryCache.Set(ItemKey, ItemValue, DateTimeOffset.UtcNow + TimeSpan.FromMinutes(60));
_cacheManager.AddOrUpdate(ItemKey, ItemValue, _ => ItemValue);
_lazyCache.Add(ItemKey, ItemValue, DateTimeOffset.UtcNow + TimeSpan.FromMinutes(60));
_fusionCache.Set(ItemKey, ItemValue, options => options.SetDuration(TimeSpan.FromMinutes(60)));

Services.CacheManager.SuspendEviction<string, string>();
}
Expand Down Expand Up @@ -62,6 +65,12 @@ public string TryGetLazyCache()
return _lazyCache.TryGetValue<string>(ItemKey, out var value) ? value : Unreachable<string>();
}

[Benchmark]
public string TryGetFusionCache()
{
return _fusionCache.GetOrDefault<string>(ItemKey) ?? Unreachable<string>();
}

[Benchmark]
public void UpdateCached()
{
Expand All @@ -85,4 +94,10 @@ public void UpdateLazyCache()
{
_lazyCache.Add(ItemKey, ItemValue, DateTimeOffset.UtcNow + TimeSpan.FromMinutes(60));
}

[Benchmark]
public void UpdateFusionCache()
{
_fusionCache.Set(ItemKey, ItemValue, options => options.SetDuration(TimeSpan.FromMinutes(60)));
}
}
70 changes: 70 additions & 0 deletions src/FastCache.Benchmarks/ComparisonAsync.cs
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);
}
}
1 change: 1 addition & 0 deletions src/FastCache.Benchmarks/FastCache.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="LazyCache" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="RangeExtensions" Version="2.1.1" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.23.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 5b5e696

Please sign in to comment.