diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-05-06 16:47:37 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2013-05-06 16:47:37 -0400 |
| commit | 05b79fd2e0e519f1fa5d67ed70a574b80736cb06 (patch) | |
| tree | 52a7ed50935e77cfb1906e0341be734835118b33 /MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs | |
| parent | c568f352eb4ff006777a22b805dd68594ca89775 (diff) | |
split rt provider into two
Diffstat (limited to 'MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs')
| -rw-r--r-- | MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index ca19e8844..23433ac75 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -134,11 +134,15 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager if (cachedInfo != null) { - var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag)) - || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow); + var now = DateTime.UtcNow; + + var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 14) + || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > now); if (isCacheValid) { + _logger.Debug("Cache is still valid for {0}", options.Url); + try { return GetCachedResponse(cachedReponsePath); @@ -180,26 +184,38 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false); - EnsureSuccessStatusCode(response); - - options.CancellationToken.ThrowIfCancellationRequested(); - if (options.EnableResponseCache) { + if (response.StatusCode != HttpStatusCode.NotModified) + { + EnsureSuccessStatusCode(response); + } + + options.CancellationToken.ThrowIfCancellationRequested(); + cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); if (response.StatusCode == HttpStatusCode.NotModified) { + _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); + return GetCachedResponse(cachedReponsePath); } - if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) + if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || + (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) { await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); return GetCachedResponse(cachedReponsePath); } } + else + { + EnsureSuccessStatusCode(response); + + options.CancellationToken.ThrowIfCancellationRequested(); + } return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); } @@ -284,6 +300,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } cachedInfo.Url = url; + cachedInfo.RequestDate = DateTime.UtcNow; var etag = response.Headers.ETag; if (etag != null) |
