Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions HttpClient.Cache.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{FCC751F0
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClient.Cache.Tests", "tests\HttpClient.Cache.Tests\HttpClient.Cache.Tests.csproj", "{AF0D7A5D-1242-44B1-9584-7D50FF6612DA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{88E718FE-AE0C-4649-A7BC-9D038FC6207A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "samples\ConsoleApp\ConsoleApp.csproj", "{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,8 +24,13 @@ Global
{AF0D7A5D-1242-44B1-9584-7D50FF6612DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF0D7A5D-1242-44B1-9584-7D50FF6612DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF0D7A5D-1242-44B1-9584-7D50FF6612DA}.Release|Any CPU.Build.0 = Release|Any CPU
{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AF0D7A5D-1242-44B1-9584-7D50FF6612DA} = {FCC751F0-5E00-41CC-BB68-5870C901A75F}
{2F33C2DF-0CA0-418B-9D06-8CDAA2DF41BC} = {88E718FE-AE0C-4649-A7BC-9D038FC6207A}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\HttpClient.Cache\HttpClient.Cache.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions samples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Diagnostics;
using System.Net;
using HttpClient.Cache.InMemory;

const string url = "http://worldclockapi.com/api/json/utc/now";

//Set the cache time for each required status
var cacheExpiration = new Dictionary<HttpStatusCode, TimeSpan>
{
{HttpStatusCode.OK, TimeSpan.FromSeconds(60)},
{HttpStatusCode.BadRequest, TimeSpan.FromSeconds(10)},
{HttpStatusCode.InternalServerError, TimeSpan.FromSeconds(5)}
};

//Client calls API and caches it
//Report will show 1 Miss (initial) and 4 Hits.
var innerHandler = new HttpClientHandler();
var cacheHandler = new InMemoryCacheHandler(innerHandler, cacheExpiration);
using (var httpClient = new System.Net.Http.HttpClient(cacheHandler))
{
for (int i = 1; i <= 5; ++i)
{
Console.Write($"Try: {i}: {url} ");

var stopwatch = Stopwatch.StartNew();
var result = await httpClient.GetAsync(url);
Console.Write($" --> {result.StatusCode} ");
stopwatch.Stop();

Console.WriteLine($"Done in: {stopwatch.ElapsedMilliseconds} ms");
await Task.Delay(TimeSpan.FromSeconds(1));
}
}

var stats = cacheHandler.StatsProvider.GetReport();
Console.WriteLine($"Cache stats - total requests: {stats.Total.TotalRequests}");
Console.WriteLine($"--> Hit: {stats.Total.CacheHit} [{stats.Total.TotalHitsPercent}]");
Console.WriteLine($"--> Miss: {stats.Total.CacheMiss} [{stats.Total.TotalMissPercent}]");
Console.ReadLine();