11using Microsoft . Extensions . Caching . Distributed ;
2- using Microsoft . Extensions . Caching . Memory ;
32using Microsoft . Extensions . DependencyInjection ;
43using SerializationDemo . Client . Serialization ;
54using SerializationDemo . Client . Serialization . Json ;
98namespace SerializationDemo . Client . Clients
109{
1110 internal class CachedJsonClient : ClientBase , IClient
12- {
11+ {
1312 private readonly IByteSerializer _serializer = new JsonByteSerializer ( ) ;
1413 private readonly HttpClient _client = new HttpClient ( ) ;
1514 private readonly IDistributedCache _distributedCache ;
1615 private const string AllEmployeesKey = "AllEmployees" ;
1716
1817 public CachedJsonClient ( )
19- {
18+ {
2019 var collection = new ServiceCollection ( ) ;
2120 collection . AddDistributedMemoryCache ( ) ;
2221 IServiceProvider serviceProvider = collection . BuildServiceProvider ( ) ;
2322
24- _distributedCache = serviceProvider . GetService < IDistributedCache > ( ) ;
23+ _distributedCache = serviceProvider . GetRequiredService < IDistributedCache > ( ) ;
2524 }
2625
2726 public async Task < List < Employee > > GetAllEmployees ( )
2827 {
2928 // Find cached item
30- byte [ ] objectFromCache = await _distributedCache . GetAsync ( AllEmployeesKey ) ;
29+ byte [ ] ? objectFromCache = await _distributedCache . GetAsync ( AllEmployeesKey ) ;
3130
3231 if ( objectFromCache != null )
33- {
32+ {
3433 var employees = _serializer . Deserialize < List < Employee > > ( objectFromCache ) ;
35- return employees ;
34+ return employees ?? [ ] ;
3635 }
3736
3837 return await RefreshCache ( ) ;
@@ -43,25 +42,25 @@ private async Task<HttpContent> GetResponseFromApi()
4342 var url = $ "{ BaseUrl } /api/employees";
4443 var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
4544 request . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
46-
45+
4746 var response = await _client . SendAsync ( request ) ;
48-
49- return response . Content ;
47+
48+ return response . Content ;
5049 }
5150
52- public async Task < Employee > CreateEmployee ( Employee employee )
51+ public async Task < Employee ? > CreateEmployee ( Employee employee )
5352 {
5453 var url = $ "{ BaseUrl } /api/employees";
5554 var request = new HttpRequestMessage ( HttpMethod . Post , url ) ;
5655 request . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
5756 request . Content = new ByteArrayContent ( _serializer . Serialize ( employee ) ) ;
58- request . Content . Headers . Add ( "Content-Type" , "application/json" ) ;
59-
57+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ;
58+
6059 var response = await _client . SendAsync ( request ) ;
6160
6261 if ( response . IsSuccessStatusCode )
6362 {
64- await RefreshCache ( ) ;
63+ await RefreshCache ( ) ;
6564 }
6665
6766 return employee ;
@@ -71,16 +70,17 @@ private async Task<List<Employee>> RefreshCache()
7170 {
7271 // If not found, then recalculate response
7372 var content = await GetResponseFromApi ( ) ;
73+ var employees = _serializer . Deserialize < List < Employee > > ( await content . ReadAsByteArrayAsync ( ) ) ?? [ ] ;
7474
7575 var cacheEntryOptions = new DistributedCacheEntryOptions ( )
76- . SetSlidingExpiration ( TimeSpan . FromSeconds ( 10 ) )
77- . SetAbsoluteExpiration ( TimeSpan . FromSeconds ( 30 ) ) ;
76+ . SetSlidingExpiration ( TimeSpan . FromSeconds ( 10 ) )
77+ . SetAbsoluteExpiration ( TimeSpan . FromSeconds ( 30 ) ) ;
7878
7979 // Cache it
80- var resultAsByteArray = _serializer . Serialize ( await content . ReadAsStringAsync ( ) ) ;
80+ var resultAsByteArray = _serializer . Serialize ( employees ) ;
8181 await _distributedCache . SetAsync ( AllEmployeesKey , resultAsByteArray , cacheEntryOptions ) ;
8282
83- return _serializer . Deserialize < List < Employee > > ( resultAsByteArray ) ;
83+ return employees ;
8484 }
8585 }
86- }
86+ }
0 commit comments