Skip to content

Commit 0cc6694

Browse files
Reformat sample code
1 parent 2c080d3 commit 0cc6694

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Samples/ConsoleAppSample/Program.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22
using System.Diagnostics;
33
using System.Net.Http;
44
using System.Threading;
5+
using System.Threading.Tasks;
56
using Microsoft.Extensions.Caching.Abstractions;
67
using Microsoft.Extensions.Caching.InMemory;
78

89
namespace ConsoleAppSample
910
{
1011
internal class Program
1112
{
12-
private static void Main(string[] args)
13+
private static async Task Main(string[] args)
1314
{
1415
const string url = "http://worldtimeapi.org/api/timezone/Europe/Zurich";
1516

1617
// HttpClient uses an HttpClientHandler nested into InMemoryCacheHandler in order to handle http get response caching
1718
var httpClientHandler = new HttpClientHandler();
18-
var cacheExpirationPerHttpResponseCode = CacheExpirationProvider.CreateSimple(TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(5));
19-
var handler = new InMemoryCacheHandler(httpClientHandler, cacheExpirationPerHttpResponseCode);
20-
using (var client = new HttpClient(handler))
19+
20+
var cacheExpirationPerHttpResponseCode = CacheExpirationProvider.CreateSimple(
21+
success: TimeSpan.FromSeconds(60),
22+
clientError: TimeSpan.FromSeconds(10),
23+
serverError: TimeSpan.FromSeconds(5));
24+
25+
var inMemoryCacheHandler = new InMemoryCacheHandler(httpClientHandler, cacheExpirationPerHttpResponseCode);
26+
27+
using (var httpClient = new HttpClient(inMemoryCacheHandler))
2128
{
2229
// HttpClient calls the same API endpoint five times:
2330
// - The first attempt is called against the real API endpoint since no cache is available
@@ -26,10 +33,10 @@ private static void Main(string[] args)
2633
{
2734
Console.Write($"Attempt {i}: HTTP GET {url}...");
2835
var stopwatch = Stopwatch.StartNew();
29-
var result = client.GetAsync(url).GetAwaiter().GetResult();
36+
var httpResponseMessage = await httpClient.GetAsync(url);
3037

3138
// Do something useful with the returned content...
32-
var content = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();
39+
var httpResponseContent = await httpResponseMessage.Content.ReadAsStringAsync();
3340
Console.WriteLine($" completed in {stopwatch.ElapsedMilliseconds}ms");
3441

3542
// Artificial wait time...
@@ -39,8 +46,9 @@ private static void Main(string[] args)
3946

4047
Console.WriteLine();
4148

42-
var stats = handler.StatsProvider.GetStatistics();
43-
Console.WriteLine($"TotalRequests: {stats.Total.TotalRequests}");
49+
var stats = inMemoryCacheHandler.StatsProvider.GetStatistics();
50+
Console.WriteLine($"Statistics:");
51+
Console.WriteLine($"-> TotalRequests: {stats.Total.TotalRequests}");
4452
Console.WriteLine($"-> CacheHit: {stats.Total.CacheHit}");
4553
Console.WriteLine($"-> CacheMiss: {stats.Total.CacheMiss}");
4654
Console.ReadKey();

0 commit comments

Comments
 (0)