2
2
using System . Diagnostics ;
3
3
using System . Net . Http ;
4
4
using System . Threading ;
5
+ using System . Threading . Tasks ;
5
6
using Microsoft . Extensions . Caching . Abstractions ;
6
7
using Microsoft . Extensions . Caching . InMemory ;
7
8
8
9
namespace ConsoleAppSample
9
10
{
10
11
internal class Program
11
12
{
12
- private static void Main ( string [ ] args )
13
+ private static async Task Main ( string [ ] args )
13
14
{
14
15
const string url = "http://worldtimeapi.org/api/timezone/Europe/Zurich" ;
15
16
16
17
// HttpClient uses an HttpClientHandler nested into InMemoryCacheHandler in order to handle http get response caching
17
18
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 ) )
21
28
{
22
29
// HttpClient calls the same API endpoint five times:
23
30
// - 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)
26
33
{
27
34
Console . Write ( $ "Attempt { i } : HTTP GET { url } ...") ;
28
35
var stopwatch = Stopwatch . StartNew ( ) ;
29
- var result = client . GetAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ;
36
+ var httpResponseMessage = await httpClient . GetAsync ( url ) ;
30
37
31
38
// Do something useful with the returned content...
32
- var content = result . Content . ReadAsStringAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
39
+ var httpResponseContent = await httpResponseMessage . Content . ReadAsStringAsync ( ) ;
33
40
Console . WriteLine ( $ " completed in { stopwatch . ElapsedMilliseconds } ms") ;
34
41
35
42
// Artificial wait time...
@@ -39,8 +46,9 @@ private static void Main(string[] args)
39
46
40
47
Console . WriteLine ( ) ;
41
48
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 } ") ;
44
52
Console . WriteLine ( $ "-> CacheHit: { stats . Total . CacheHit } ") ;
45
53
Console . WriteLine ( $ "-> CacheMiss: { stats . Total . CacheMiss } ") ;
46
54
Console . ReadKey ( ) ;
0 commit comments