diff --git a/src/Marvin.HttpCache/HttpCacheHandler.cs b/src/Marvin.HttpCache/HttpCacheHandler.cs index 58fd113..c96b562 100644 --- a/src/Marvin.HttpCache/HttpCacheHandler.cs +++ b/src/Marvin.HttpCache/HttpCacheHandler.cs @@ -101,7 +101,7 @@ protected override Task SendAsync(HttpRequestMessage reques } - private Task HandleHttpPutOrPatch(HttpRequestMessage request, + private async Task HandleHttpPutOrPatch(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { @@ -118,11 +118,11 @@ private Task HandleHttpPutOrPatch(HttpRequestMessage reques HttpResponseMessage responseFromCache = null; // available in cache? - var responseFromCacheAsTask = _cacheStore.GetAsync(cacheKey); - if (responseFromCacheAsTask.Result != null) + var cacheEntry = await _cacheStore.GetAsync(cacheKey); + if (cacheEntry != null) { - addCachingHeaders = true; - responseFromCache = responseFromCacheAsTask.Result.HttpResponse; + addCachingHeaders = true; + responseFromCache = cacheEntry.HttpResponse; } if (addCachingHeaders) @@ -143,11 +143,11 @@ private Task HandleHttpPutOrPatch(HttpRequestMessage reques } } - return HandleSendAndContinuationForPutPatch(cacheKey, request, cancellationToken); + return await HandleSendAndContinuationForPutPatch(cacheKey, request, cancellationToken); } - private Task HandleHttpGet(HttpRequestMessage request, + private async Task HandleHttpGet(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { // get VaryByHeaders - order in the request shouldn't matter, so order them so the @@ -167,19 +167,17 @@ private Task HandleHttpGet(HttpRequestMessage request, if (request.Headers.CacheControl != null && request.Headers.CacheControl.NoCache) { // Don't get from cache. Get from server. - return HandleSendAndContinuation( + return await HandleSendAndContinuation( CacheKeyHelpers.CreateCacheKey(primaryCacheKey), request, cancellationToken, false); } - // available in cache? - var cacheEntriesFromCacheAsTask = _cacheStore.GetAsync(primaryCacheKey); - if (cacheEntriesFromCacheAsTask.Result != default(IEnumerable)) + // available in cache? + cacheEntriesFromCache = await _cacheStore.GetAsync(primaryCacheKey); + if (cacheEntriesFromCache != default(IEnumerable)) { - cacheEntriesFromCache = cacheEntriesFromCacheAsTask.Result; - // TODO: for all of these, check the varyby headers (secondary key). // An item is a match if secondary & primary keys both match! responseFromCache = cacheEntriesFromCache.First().HttpResponse; @@ -217,20 +215,20 @@ private Task HandleHttpGet(HttpRequestMessage request, responseFromCache.Content.Headers.LastModified.Value.ToString("r")); } - return HandleSendAndContinuation( + return await HandleSendAndContinuation( CacheKeyHelpers.CreateCacheKey(primaryCacheKey), request, cancellationToken, true); } else { // response is allowed to be cached and there's // no need to revalidate: return the cached response - return Task.FromResult(responseFromCache); + return responseFromCache; } } else { // response isn't cached. Get it, and (possibly) add it to cache. - return HandleSendAndContinuation( + return await HandleSendAndContinuation( CacheKeyHelpers.CreateCacheKey(primaryCacheKey), request, cancellationToken, false); } @@ -238,22 +236,17 @@ private Task HandleHttpGet(HttpRequestMessage request, } - private Task HandleSendAndContinuation(CacheKey cacheKey, HttpRequestMessage request, + private async Task HandleSendAndContinuation(CacheKey cacheKey, HttpRequestMessage request, System.Threading.CancellationToken cancellationToken, bool mustRevalidate) { - return base.SendAsync(request, cancellationToken) - .ContinueWith( - task => - { - - var serverResponse = task.Result; + var serverResponse = await base.SendAsync(request, cancellationToken); // if we had to revalidate & got a 304 returned, that means // we can get the response message from cache. if (mustRevalidate && serverResponse.StatusCode == HttpStatusCode.NotModified) - { - var cacheEntry = _cacheStore.GetAsync(cacheKey).Result; + { + var cacheEntry = await _cacheStore.GetAsync(cacheKey); var responseFromCacheEntry = cacheEntry.HttpResponse; responseFromCacheEntry.RequestMessage = request; @@ -274,8 +267,8 @@ private Task HandleSendAndContinuation(CacheKey cacheKey, H if (isCacheable) { - // add the response to cache - _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse)); + // add the response to cache + await _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse)); } @@ -284,22 +277,17 @@ private Task HandleSendAndContinuation(CacheKey cacheKey, H } return serverResponse; - }); + } - private Task HandleSendAndContinuationForPutPatch(CacheKey cacheKey, HttpRequestMessage request, + private async Task HandleSendAndContinuationForPutPatch(CacheKey cacheKey, HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) - { - - return base.SendAsync(request, cancellationToken) - .ContinueWith( - task => - { - - var serverResponse = task.Result; + { + + var serverResponse = await base.SendAsync(request, cancellationToken); if (serverResponse.IsSuccessStatusCode) { @@ -322,10 +310,10 @@ private Task HandleSendAndContinuationForPutPatch(CacheKey // guarantee the new response is cacheable. // // - look for resources in cache that start with - // the cachekey + "?" for querystring. - - _cacheStore.RemoveAsync(cacheKey); - _cacheStore.RemoveRangeAsync(cacheKey.PrimaryKey + "?"); + // the cachekey + "?" for querystring. + + await _cacheStore.RemoveAsync(cacheKey); + await _cacheStore.RemoveRangeAsync(cacheKey.PrimaryKey + "?"); } @@ -334,8 +322,8 @@ private Task HandleSendAndContinuationForPutPatch(CacheKey if (isCacheable) { - // add the response to cache - _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse)); + // add the response to cache + await _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse)); } // what about vary by headers (=> key should take this into account)? @@ -343,8 +331,6 @@ private Task HandleSendAndContinuationForPutPatch(CacheKey } return serverResponse; - - }); } }