WebRequest 不会使缓存项目在同一进程中可用

发布于 2024-09-26 05:28:59 字数 1215 浏览 6 评论 0原文

我们看到了一种奇怪的行为,即由于使用 WebRequest API 而被缓存(在 IE/wininet 缓存中)的内容在创建新进程之前无法从缓存中获得。

考虑以下代码:

using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Threading;

namespace HttpCachingTest {
    class Program {
        static void Main(string[] args) {
            MakeRequest();
            Thread.Sleep(1000);
            MakeRequest();
        }

        private static void MakeRequest() {
            var request = (HttpWebRequest)WebRequest.Create("http://a0.twimg.com/a/1286210883/images/twitter_logo_header.png");
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);

            var response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(String.Format("IsFromCache: {0}, ContentLength: {1}", response.IsFromCache, response.ContentLength));

            using (var fileStream = File.OpenWrite("test.jpg")) {
                response.GetResponseStream().CopyTo(fileStream);
            }
        }
    }
}

我们看到的行为是,对于第二个请求,IsFromCache 为 false。但是,如果我们第二次运行该应用程序,则 IsFromCache 为 true。这意味着事物被正确地缓存,但是那些刚刚缓存的事物对于当前进程来说是不可用的,这没有什么意义。

任何人都可以解释这种行为,并知道如何修复它以便缓存的项目可以立即被命中?

We're seeing a strange behavior where things that get cached (in the IE/wininet cache) as a result of using the WebRequest API are not available from the cache until a new process is created.

Consider the following code:

using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Threading;

namespace HttpCachingTest {
    class Program {
        static void Main(string[] args) {
            MakeRequest();
            Thread.Sleep(1000);
            MakeRequest();
        }

        private static void MakeRequest() {
            var request = (HttpWebRequest)WebRequest.Create("http://a0.twimg.com/a/1286210883/images/twitter_logo_header.png");
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);

            var response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(String.Format("IsFromCache: {0}, ContentLength: {1}", response.IsFromCache, response.ContentLength));

            using (var fileStream = File.OpenWrite("test.jpg")) {
                response.GetResponseStream().CopyTo(fileStream);
            }
        }
    }
}

The behavior we see is that for the second request, IsFromCache is false. However, if we run the app a second time, then IsFromCache is true. This implies that things are correctly being cached, but those things that were just cached are not available to the current process, which makes very little sense.

Can anyone explain this behavior, and know how it can be fixed so that cached items can get hit right away?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2024-10-03 05:28:59

您不会处理 HttpWebResponseGetResponseStream() 返回的流。因此,它似乎尚未完成对请求的处理,并且它获得的数据无法安全地存储在缓存中,因为它可能不是完整的实体。当您的进程结束时,终结器将修复此问题,因此下次运行时它将位于缓存中。

正确处理一次性对象,缓存行为将符合预期。

You aren't disposing of the HttpWebResponse nor the stream returned by GetResponseStream(). It appears therefore to not have completed processing the request, and the data it obtained can't be safely stored in the cache, as it may not be a full entity. When your process ends, the finalisers will fix this, so it'll be in the cache for the next time it runs.

Dispose up your disposable objects correctly, and the caching behaviour will be as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文