Ejemplo n.º 1
0
        private Task <HttpResponseMessage> HandleSendAndContinuationForPutPatch(CacheKey cacheKey, HttpRequestMessage request,
                                                                                System.Threading.CancellationToken cancellationToken)
        {
            return(base.SendAsync(request, cancellationToken)
                   .ContinueWith(
                       task =>
            {
                var serverResponse = task.Result;

                if (serverResponse.IsSuccessStatusCode)
                {
                    // ensure no NULL dates
                    if (serverResponse.Headers.Date == null)
                    {
                        serverResponse.Headers.Date = DateTimeOffset.UtcNow;
                    }

                    // should we clear?

                    if ((_enableClearRelatedResourceRepresentationsAfterPut && request.Method == HttpMethod.Put)
                        ||
                        (_enableClearRelatedResourceRepresentationsAfterPatch && request.Method.Method.ToLower() == "patch"))
                    {
                        // clear related resources
                        //
                        // - remove resource with cachekey.  This must be done, as there's no
                        // 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 + "?");
                    }


                    // check the response: is this response allowed to be cached?
                    bool isCacheable = HttpResponseHelpers.CanBeCached(serverResponse);

                    if (isCacheable)
                    {
                        // add the response to cache
                        _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse));
                    }

                    // what about vary by headers (=> key should take this into account)?
                }

                return serverResponse;
            }));
        }
Ejemplo n.º 2
0
        private Task <HttpResponseMessage> HandleSendAndContinuation(CacheKey cacheKey, HttpRequestMessage request,
                                                                     System.Threading.CancellationToken cancellationToken, bool mustRevalidate)
        {
            return(base.SendAsync(request, cancellationToken)
                   .ContinueWith(
                       task =>
            {
                var serverResponse = task.Result;

                // 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 responseFromCacheEntry = cacheEntry.HttpResponse;
                    responseFromCacheEntry.RequestMessage = request;

                    return responseFromCacheEntry;
                }

                if (serverResponse.IsSuccessStatusCode)
                {
                    // ensure no NULL dates
                    if (serverResponse.Headers.Date == null)
                    {
                        serverResponse.Headers.Date = DateTimeOffset.UtcNow;
                    }

                    // check the response: is this response allowed to be cached?
                    bool isCacheable = HttpResponseHelpers.CanBeCached(serverResponse);

                    if (isCacheable)
                    {
                        // add the response to cache
                        _cacheStore.SetAsync(cacheKey, new CacheEntry(serverResponse));
                    }


                    // what about vary by headers (=> key should take this into account)?
                }

                return serverResponse;
            }));
        }