Skip to content
Open
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
76 changes: 31 additions & 45 deletions src/Marvin.HttpCache/HttpCacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques

}

private Task<HttpResponseMessage> HandleHttpPutOrPatch(HttpRequestMessage request,
private async Task<HttpResponseMessage> HandleHttpPutOrPatch(HttpRequestMessage request,
System.Threading.CancellationToken cancellationToken)
{

Expand All @@ -118,11 +118,11 @@ private Task<HttpResponseMessage> 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)
Expand All @@ -143,11 +143,11 @@ private Task<HttpResponseMessage> HandleHttpPutOrPatch(HttpRequestMessage reques
}
}

return HandleSendAndContinuationForPutPatch(cacheKey, request, cancellationToken);
return await HandleSendAndContinuationForPutPatch(cacheKey, request, cancellationToken);
}


private Task<HttpResponseMessage> HandleHttpGet(HttpRequestMessage request,
private async Task<HttpResponseMessage> HandleHttpGet(HttpRequestMessage request,
System.Threading.CancellationToken cancellationToken)
{
// get VaryByHeaders - order in the request shouldn't matter, so order them so the
Expand All @@ -167,19 +167,17 @@ private Task<HttpResponseMessage> 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<CacheEntry>))
// available in cache?
cacheEntriesFromCache = await _cacheStore.GetAsync(primaryCacheKey);
if (cacheEntriesFromCache != default(IEnumerable<CacheEntry>))
{
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;
Expand Down Expand Up @@ -217,43 +215,38 @@ private Task<HttpResponseMessage> 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);
}


}


private Task<HttpResponseMessage> HandleSendAndContinuation(CacheKey cacheKey, HttpRequestMessage request,
private async Task<HttpResponseMessage> 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;

Expand All @@ -274,8 +267,8 @@ private Task<HttpResponseMessage> 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));
}


Expand All @@ -284,22 +277,17 @@ private Task<HttpResponseMessage> HandleSendAndContinuation(CacheKey cacheKey, H
}

return serverResponse;
});

}




private Task<HttpResponseMessage> HandleSendAndContinuationForPutPatch(CacheKey cacheKey, HttpRequestMessage request,
private async Task<HttpResponseMessage> 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)
{
Expand All @@ -322,10 +310,10 @@ private Task<HttpResponseMessage> 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 + "?");
}


Expand All @@ -334,17 +322,15 @@ private Task<HttpResponseMessage> 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)?

}

return serverResponse;

});
}

}
Expand Down