WebRequest 不会使缓存项目在同一进程中可用
我们看到了一种奇怪的行为,即由于使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会处理
HttpWebResponse
或GetResponseStream()
返回的流。因此,它似乎尚未完成对请求的处理,并且它获得的数据无法安全地存储在缓存中,因为它可能不是完整的实体。当您的进程结束时,终结器将修复此问题,因此下次运行时它将位于缓存中。正确处理一次性对象,缓存行为将符合预期。
You aren't disposing of the
HttpWebResponse
nor the stream returned byGetResponseStream()
. 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.