コード例 #1
0
        private void OnResultsFilterResponse(WebResponse webRes, object response, string httpMethod, string requestUri, object request)
        {
            existingResultsFilterResponse?.Invoke(webRes, response, httpMethod, requestUri, request);

            if (httpMethod != HttpMethods.Get || response == null || webRes == null)
            {
                return;
            }

            var eTag            = webRes.Headers[HttpHeaders.ETag];
            var lastModifiedStr = webRes.Headers[HttpHeaders.LastModified];

            if (eTag == null && lastModifiedStr == null)
            {
                return;
            }

            var entry = new HttpCacheEntry(response)
            {
                ETag          = eTag,
                ContentLength = webRes.ContentLength >= 0 ? webRes.ContentLength : (long?)null,
            };

            if (lastModifiedStr != null)
            {
                DateTime lastModified;
                if (DateTime.TryParse(lastModifiedStr, new DateTimeFormatInfo(), DateTimeStyles.RoundtripKind, out lastModified))
                {
                    entry.LastModified = lastModified.ToUniversalTime();
                }
            }

            long secs;
            var  ageStr = webRes.Headers[HttpHeaders.Age];

            if (ageStr != null && long.TryParse(ageStr, out secs))
            {
                entry.Age = TimeSpan.FromSeconds(secs);
            }

            var cacheControl = webRes.Headers[HttpHeaders.CacheControl];

            if (cacheControl != null)
            {
                var parts = cacheControl.Split(',');

                foreach (var part in parts)
                {
                    var kvp = part.Split('=');
                    var key = kvp[0].Trim().ToLower();

                    switch (key)
                    {
                    case "max-age":
                        if (kvp.Length == 2 && long.TryParse(kvp[1], out secs))
                        {
                            entry.MaxAge = TimeSpan.FromSeconds(secs);
                        }
                        break;

                    case "must-revalidate":
                        entry.MustRevalidate = true;
                        break;

                    case "no-cache":
                        entry.NoCache = true;
                        break;
                    }
                }

                entry.SetMaxAge(entry.MaxAge);
                cache[requestUri] = entry;
                Interlocked.Increment(ref cachesAdded);

                var runCleanupAfterEvery = CleanCachesWhenCountExceeds;
                if (cachesAdded % runCleanupAfterEvery == 0 &&
                    cache.Count > CleanCachesWhenCountExceeds)
                {
                    if (ClearExpiredCachesOlderThan != null)
                    {
                        RemoveExpiredCachesOlderThan(ClearExpiredCachesOlderThan.Value);
                    }
                    if (ClearCachesOlderThan != null)
                    {
                        RemoveCachesOlderThan(ClearCachesOlderThan.Value);
                    }
                }
            }
        }
        private void OnResultsFilterResponse(HttpResponseMessage webRes, object response, string httpMethod, string requestUri, object request)
        {
            if (existingResultsFilterResponse != null)
            {
                existingResultsFilterResponse(webRes, response, httpMethod, requestUri, request);
            }

            if (httpMethod != HttpMethods.Get || response == null || webRes == null)
            {
                return;
            }

            var eTag = webRes.Headers.ETag?.Tag;

            if (eTag == null && webRes.Content.Headers.LastModified == null)
            {
                return;
            }

            var entry = new HttpCacheEntry(response)
            {
                ETag          = eTag,
                ContentLength = webRes.Content.Headers.ContentLength
            };

            if (webRes.Content.Headers.LastModified != null)
            {
                entry.LastModified = webRes.Content.Headers.LastModified.Value.UtcDateTime;
            }

            entry.Age = webRes.Headers.Age;

            var cacheControl = webRes.Headers.CacheControl;

            if (cacheControl != null)
            {
                if (cacheControl.MaxAge != null)
                {
                    entry.MaxAge = cacheControl.MaxAge.Value;
                }

                entry.MustRevalidate = cacheControl.MustRevalidate;
                entry.NoCache        = cacheControl.NoCache;

                entry.SetMaxAge(entry.MaxAge);
                cache[requestUri] = entry;
                Interlocked.Increment(ref cachesAdded);

                var runCleanupAfterEvery = CleanCachesWhenCountExceeds;
                if (cachesAdded % runCleanupAfterEvery == 0 &&
                    cache.Count > CleanCachesWhenCountExceeds)
                {
                    if (ClearExpiredCachesOlderThan != null)
                    {
                        RemoveExpiredCachesOlderThan(ClearExpiredCachesOlderThan.Value);
                    }
                    if (ClearCachesOlderThan != null)
                    {
                        RemoveCachesOlderThan(ClearCachesOlderThan.Value);
                    }
                }
            }
        }